How do I check if a string contains a specific word?
snippet in php

How do I check if a string contains a specific word?

user2968

// returns true if $needle is a substring of $haystack
function contains($needle, $haystack)
{
    return strpos($haystack, $needle) !== false;
}

How do I check if a string contains a specific word?

user2800

<?php

$haystack = 'how are you';
$needle = 'are';

if (strpos($haystack,$needle) !== false) {
    echo "$haystack contains $needle";
}

?>

How do I check if a string contains a specific word?

user465

function containsWord($str, $word)
{
    return !!preg_match('#\\b' . preg_quote($word, '#') . '\\b#i', $str);
}

How do I check if a string contains a specific word?

user7244

if (stripos($string,$stringToSearch) !== false) {
    echo 'true';
}

How do I check if a string contains a specific word?

user6770

if (substr_count($a, 'are') > 0) {
    echo "at least one 'are' is present!";
}

How do I check if a string contains a specific word?

user6977

if (mb_strpos($a,'are') !== false) {
    echo 'true';
}

How do I check if a string contains a specific word?

user6161

<?php
    $mystring = 'abc';
    $findme   = 'a';
    $pos = strpos($mystring, $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 do I check if a string contains a specific word?

user7217

if (preg_match('/(are)/', $a)) {
   echo 'true';
}

How do I check if a string contains a specific word?

user1376

if (strlen(strstr($haystack,$needle))>0) {
// Needle Found
}

How do I check if a string contains a specific word?

user6839

$string = 'How are you?';
$array = explode(" ", $string);

if (in_array('are', $array) ) {
    echo 'Found the word';
}

How do I check if a string contains a specific word?

user7760

$badWords = array("dette", "capitale", "rembourser", "ivoire", "mandat");

$string = "a string with the word ivoire";

$matchFound = preg_match_all("/\b(" . implode($badWords,"|") . ")\b/i", $string, $matches);

if ($matchFound) {
    echo "a bad word has been found";
}
else {
    echo "your string is okay";
}

How do I check if a string contains a specific word?

user818

$result = stripos("I love PHP, I love PHP too!", "php");
if ($result === false) {
    // Word does not exist
}
else {
    // Word exists
}

How do I check if a string contains a specific word?

user4533

$result = false!==strpos($a, 'are');

How do I check if a string contains a specific word?

user6407

<?php
$grass = "This is pratik joshi";
$needle = "pratik";
if (stripos($grass,$needle) !== false) { 

 /*If i EXCLUDE : !== false then if string is found at 0th location, 
   still it will say STRING NOT FOUND as it will return '0' and it      
   will goto else and will say NOT Found though it is found at 0th location.*/
    echo 'Contains word';
}else{
    echo "does NOT contain word";
}
?>

How do I check if a string contains a specific word?

user7988

<?php
    $a = 'How are you?';
    if (strstr($a,'are'))  // Case sensitive
        echo 'true';
    if (stristr($a,'are'))  // Case insensitive
        echo 'true';
?>

How do I check if a string contains a specific word?

user3570

<?php
    findWord('Test all OK');

    function findWord($text) {
        if (strstr($text, 'ok')) {
            echo 'Found a word';
        }
        else
        {
            echo 'Did not find a word';
        }
    }
?>

How do I check if a string contains a specific word?

user8957

if (strpos($text, 'string_name') !== false){
   echo 'get the string';
}

How do I check if a string contains a specific word?

user8969

$a = 'Hello world?';

if (strpos($a, 'Hello') !== false) { //PAY ATTENTION TO !==, not !=
    echo 'true';
}
if (stripos($a, 'HELLO') !== false) { //Case insensitive
    echo 'true';
}

php find if substring is in string

user2730

$result = strpos("haystack", "needle");

if ($result != false)
{
  // text found
}