How can I check if a word is contained in another string using PHP?

PhpString

Php Problem Overview


Pseudo Code

text = "I go to school";
word = "to"
if ( word.exist(text) ) {
    return true ;
else {
    return false ;
}

I am looking for a PHP function which returns true if the word exists in the text.

Php Solutions


Solution 1 - Php

You have a few options depending on your needs. For this simple example, strpos() is probably the simplest and most direct function to use. If you need to do something with the result, you may prefer strstr() or preg_match(). If you need to use a complex pattern instead of a string as your needle, you'll want preg_match().

$needle = "to";
$haystack = "I go to school";

strpos() and stripos() method (stripos() is case insensitive):

if (strpos($haystack, $needle) !== false) echo "Found!";

strstr() and stristr() method (stristr is case insensitive):

if (strstr($haystack, $needle)) echo "Found!";

preg_match method (regular expressions, much more flexible but runs slower):

if (preg_match("/to/", $haystack)) echo "Found!";

Because you asked for a complete function, this is how you'd put that together (with default values for needle and haystack):

function match_my_string($needle = 'to', $haystack = 'I go to school') {
  if (strpos($haystack, $needle) !== false) return true;
  else return false;
}

PHP 8.0.0 now contains a str_contains function that works like so:

if (str_contains($haystack, $needle)) {
    echo "Found";
}

Solution 2 - Php

function hasWord($word, $txt) {
    $patt = "/(?:^|[^a-zA-Z])" . preg_quote($word, '/') . "(?:$|[^a-zA-Z])/i";
    return preg_match($patt, $txt);
}

If $word is "to", this will match:

  • "Listen to Me"
  • "To the moon"
  • "up-to-the-minute"

but not:

  • "Together"
  • "Into space"

Solution 3 - Php

use:

return (strpos($text,$word) !== false); //case-sensitive

or

return (stripos($text,$word) !== false); //case-insensitive

Solution 4 - Php

strpos

<?php
$text = "I go to school";
$word = "to"
$pos = strpos($text, $word);

if ($pos === false) {
    return false;
} else {
    return true;
}
?>

Solution 5 - Php

$text="I go to school";
return (strpos($text, 'to')!== false);

http://www.php.net/strpos">The manual page you need to find the correct usage of strpos

Solution 6 - Php

Another way (besides the strpos examples already given is to use the 'strstr' function:

if (strstr($haystack, $needle)) {
   return true;
} else {
   return false;
}

Solution 7 - Php

PHP 8 New Function str_contains

if (str_contains('Foo Bar Baz', 'Foo')) {
  echo 'Found';
}

OR

Use strpos function in php .

$text = "I go to school";
$word = "to"
if (strpos($text,$word) !== false ) {
    echo 'true';
}

Solution 8 - Php

You can use these string functions,

strstr — Find the first occurrence of a string

stristr — Case-insensitive strstr()

strrchr — Find the last occurrence of a character in a string

strpos — Find the position of the first occurrence of a substring in a string

strpbrk — Search a string for any of a set of characters

If that doesn't help then you should use preg regular expression

preg_match — Perform a regular expression match

Solution 9 - Php

@mrclay

cant' we simply do

"/(?:^|\w+)" . preg_quote($word, '/') . "(?:$|\w+)/i"

so that it either checks starting or whitespace, and ending or whitespace.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWaseemView Question on Stackoverflow
Solution 1 - Phppix0rView Answer on Stackoverflow
Solution 2 - PhpSteve ClayView Answer on Stackoverflow
Solution 3 - PhpJonathan FinglandView Answer on Stackoverflow
Solution 4 - PhpjitterView Answer on Stackoverflow
Solution 5 - PhpEinekiView Answer on Stackoverflow
Solution 6 - PhpylebreView Answer on Stackoverflow
Solution 7 - PhpArshid KVView Answer on Stackoverflow
Solution 8 - Phpmanish1706View Answer on Stackoverflow
Solution 9 - PhpacpmasqueradeView Answer on Stackoverflow