string strstr ( string $haystack , mixed $needle [, bool $before_needle ] )
(PHP 4, PHP 5)

$before_needle 은 5.3부터 사용가능하며, 반환 값은 매치되는 문자열을 반환합니다. 대소문자 구분 없이 문자열을 찾고자 한다면, stristr 함수를 사용하세요.

<?php
 $email  = 'name@test.com';
 $domain = strstr($email, '@');
 echo $domain; // 결과: @test.com

 // PHP 5.3.0 부터
 $user = strstr($email, '@', true);
 echo $user; // 결과: name
 ?>

파일 확장자 php. 도 웹에서는 실행 가능한 파일로 인식하기 때문에 php?  php.  ?php 와 같은 확장자를 제한할 필요가 있습니다.

<?php 
 $file  = 'test.gif.bmp.php.';  
 // 대소문자 구분을 위해 stristr 함수를 사용.
 $ext = stristr($file, '.'); 
 echo $ext; // 결과: .gif.bmp.php. 

 if(preg_match("/php|htm|inc/", $ext, $match)){
       echo "제한되는 확장자가 포함되어 있습니다.";
 } else {
       echo "업로드 가능합니다.";
 } 
 // 결과: 제한되는 확장자가 포함되어 있습니다. 
 ?>

다음은 stripos 를 이용한 처음 문자열을 찾습니다.

<?php 
 function userStrstr($haystack, $needle) { 
    $pos = stripos($haystack, $needle); 
    if (is_int($pos)) { 
        return substr($haystack, $pos + strlen($needle)); 
    }  
    return false;
 }  

 $email = 'name@test.com'; 
 $domain = userStrstr($email, '@'); 
 echo $domain; // 결과: test.com 
 ?>

0 댓글