2016-06-24 3 views
2

왜이 코드가 반향하지 않습니까 0?하위 문자열 비교 PHP

$email = "[email protected]"; 
$ending = "com"; 

$email = preg_replace('/[^A-Za-z0-9\-]/', '', $email); 

echo substr_compare($email, $ending, strlen($ending)-strlen($email), strlen($ending)); 

나는 documentation 당 0을 기대하고 있습니다.

+3

'의 나 strlen ($ 종료) -strlen ($ 이메일)'당신의 예와 그를 기록하고 실수에 넣고 계산을하고 오프셋을 확인 비교를 시작하고 어디서 시작하십시오. – Rizier123

+1

@ Rizier123 D' oh! '-11', 나는 단지'-strlen ($ ending)'을 할 필요가 있다고 생각한다. –

+1

때로는 작은 코드 예제를 적어 적어서 실수 값/값을 넣을 때 도움이된다. – Rizier123

답변

0

"com"으로 끝나는 지 확인하고 싶습니다.

이 읽기 ​​: startsWith() and endsWith() functions in PHP


을하지만 당신은 단지 결과를 디버깅 할 경우 : 적절하게 출력 결과를 문자열로 정수를 변환하는

시도합니다.

echo (string)substr_compare($email, $ending, strlen($ending)-strlen($email), strlen($ending)); 

또는 수행

$result = substr_compare($email, $ending, strlen($ending)-strlen($email), strlen($ending)); 
var_dump($result); // or echo (string)$result;