Question

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?

Was it helpful?

Solution

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

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

OTHER TIPS

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/'));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top