-1

like I have string "I want to buy a cake with $1000"

so if I search by "1000" should be return true, but if I search "100" or "10000" it should be return false

Another example:

"I was enjoying the music" if I search "enjoy" it should be return false, but if I search "enjoying" should be return true

I was trying to use if(strpos($string, $text ) !== false) but it is not working, however it always return true, if the specific word has extra alphabet behind or in front of the word.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Kelvin
  • 1,004
  • 1
  • 10
  • 32
  • 1
    strpos will always return true because enjoy is a substring of enjoying. Read more in http://php.net/manual/en/function.strpos.php – Camilo Go Jr. Mar 11 '17 at 04:49

2 Answers2

1

I would look at regular expressions. Here is a link to a similar problem Regular expression to match a whole word There is a section that talks about word boundaries, you'll want to check that out.

Community
  • 1
  • 1
bcr666
  • 2,157
  • 1
  • 12
  • 23
1

Try this:

$haystack = "I want to buy a cake with $1000";
if (preg_match('/\$1000$/', $haystack, $match) === 1) {
  echo "Found it: ". $match[0];
}

It is also advised to escape your dollar sign, or put the string inside of single quotes as a safety measure. If there's a word character right after the '$' sign, the parser will try to expand it as a variable (by casting it to a string).

Gergely Lukacsy
  • 2,881
  • 2
  • 23
  • 28