0

I am trying to find weather string contain particular word by using php strpos function and i have tried below code

echo strpos('sfsdf/abc/this', 'sfsdf/');

though it should return true because sfsdf/ present in string I am getting 0(false).

here is live example http://codepad.viper-7.com/dqvNCR

Richerd fuld
  • 364
  • 1
  • 10
  • 23

2 Answers2

3

The 0 you are getting is not false it is because the targeted value is the first value of your string.

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

http://php.net/manual/en/function.strpos.php

Demo:

echo strpos('asdf/sfsdf/abc/this', 'sfsdf/');

Output:

5

because the sfsdf/ starts at the 6th position.

Live demo: http://codepad.viper-7.com/NgQnxL

chris85
  • 23,846
  • 7
  • 34
  • 51
1

Use stristr instead.

Returns the matched substring. If needle is not found, returns FALSE.

<?php

if(stristr('sfsdf/abc/this', 'sfsdf/'))
{
    echo 1;
}
else
{
    echo 0;
}

DEMO

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50