0

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?

mrN
  • 3,734
  • 15
  • 58
  • 82

4 Answers4

2

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

strstr($t, '/image/');
federico-t
  • 12,014
  • 19
  • 67
  • 111
2

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";
}
?>
  • How about just `if(!$pos)`? Will that omit the need of `===`? – mrN Aug 07 '13 at 02:34
  • I accepted other answer as that is the answer to my question. But I am using your solution. +1 – mrN Aug 07 '13 at 03:09
  • nooo the points! all those lovely points i have lost :-; -kidding all good ;-) –  Aug 07 '13 at 03:10
0

try like this instead

<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr($t, '/image/'));
exit;
?>
meda
  • 45,103
  • 14
  • 92
  • 122
0

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

iBet7o
  • 758
  • 8
  • 9