-2

why this code below return false ?

strstr('/example/test', 'example/test/next');

The second parameter contains a good portion of the first parameter ???

stloc
  • 1,508
  • 1
  • 16
  • 26
  • possible duplicate of [How to check if a string contains specific words?](http://stackoverflow.com/questions/4366730/how-to-check-if-a-string-contains-specific-words) – Luke Sep 12 '14 at 12:14
  • 2
    because '/example/test' does not contain 'example/test/next' – Andrii Mishchenko Sep 12 '14 at 12:15
  • 2
    And not only that, but 'example/test/next' does not contain '/example/test', either: even if you'd got the parameters the right way around, *neither* string contains the other string. (Also, if you only want to check if the string contains part of another string, you should be using strpos, like it says in [the manual](http://php.net/manual/en/function.strstr.php)) – Matt Gibson Sep 12 '14 at 12:17

2 Answers2

2

As stated in strstr

the second parameter is what you search for, so you need to change the parameters around and lose the first '/' in the second parameter.

echo(strstr('example/test/next', 'example/test'));
// Returns example/test/next
Wezy
  • 665
  • 1
  • 5
  • 14
2

Wrong order of parameters... strstr('yourstring', 'searchstring') strstr

strstr('/example/test', 'example/test/next');

Cahge to

strstr('example/test/next', '/example/test');

--> it will return false too. bacause 'example/test/next' has NO '/' at the begin..

strstr('/example/test/next', '/example/test');

--> will return '/example/test/next'

Edi G.
  • 2,432
  • 7
  • 24
  • 33