문제

I am trying to find whether a string contains a certain text or not using strstr()

$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr('/image/', $t));
exit;

But this gives false. Why is it giving fasle? How to fix it?

도움이 되었습니까?

해결책

Your parameters are inverted (see strstr). This is the correct way of using it:

strstr($t, '/image/');

다른 팁

you should use strpos , faster, less resources, from the manual with your vars

<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
$findme   = '/image/';
$pos = strpos($t, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

try like this instead

<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr($t, '/image/'));
exit;
?>

You can see the documentation of the function.

Validates the syntax.

php/documentation/function.strstr.php

The correct use would: var_dump(strstr($t, '/image/'));

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top