2013-08-28 1 views
0

PHP에서 부분 문자열을 검색하여 주어진 문자열의 끝에 올리려합니다. 예 : 'abd def'문자열에 이 있으면 def를 검색하면 끝 부분에 있으므로 true를 반환합니다. 그러나 abd를 검색하면 끝이 아니므로 false를 반환합니다.끝 부분에있는 경우 true를 반환하는 하위 문자열을 검색합니다.

가능합니까?

+0

'이 가능'YES : 마지막 x 문자 여부를이 테스트는 x 테스트 문자열의 길이를 동일 테스트 문자열을 동일. –

답변

0

가정 전체 단어 :

$match = 'def'; 
$words = explode(' ', 'abd def'); 

if (array_pop($words) == $match) { 
    ... 
} 

또는 정규식 사용 :

if (preg_match('/def$/', 'abd def')) { 
    ... 
} 
1

당신은 이것에 대한 preg_match을 사용할 수

$str = 'abd def'; 
$result = (preg_match("/def$/", $str) === 1); 
var_dump($result); 
0

이 답변에 관계없이 전체 충분히 강력한되어야한다 단어 또는 무엇이든

$match = 'def'; 
$words = 'abd def'; 

$location = strrpos($words, $match); // Find the rightmost location of $match 
$matchlength = strlen($match);  // How long is $match 

/* If the rightmost location + the length of what's being matched 
* is equal to the length of what's being searched, 
* then it's at the end of the string 
*/ 
if ($location + $matchlength == strlen($words)) { 
    ... 
} 
0

strrchr() 기능을 확인하십시오. 시도해보십시오

$word = 'abcdef'; 
$niddle = 'def'; 
if (strrchr($word, $niddle) == $niddle) { 
    echo 'true'; 
} else { 
    echo 'false'; 
} 
1

분리 기호 나 정규식으로 분리 할 필요가없는 다른 방법으로 시도하십시오.

$string = "abcdef"; 
$test = "def"; 

if(substr($string, -(strlen($test))) === $test) 
{ 
    /* logic here */ 
} 
관련 문제