PHP partial string search

How can you search for a partial string when typing (do not use MySQL), like the LIKE function in MySQL, but using PHP when searching for a string, for example.

<?php    

$string = "Stackoverflow";

$find = "overfl";

if($find == $string)
{
    return true;
}
else
{
    return false
}

?>

But this will obviously work, but there is no function where you can partially search for a string? That would be great :)

EDIT:

What if it was in an array?

if I use strpos, it does an echo; if I use it, it looks like truetruetruetruetrue.

+5
source share
3 answers

I prefer to use strpos

$needle='appy';
$haystack='I\'m feeling flappy, and you?';

if(strpos($haystack,$needle)!==false){
   //then it was found
   }

If you want it to ignore case, use stripos .

, , , 0 0. , false, , .

,

Boolean , , FALSE, 0 ". " " . === .

, strpos . Warning: strpos() expects parameter 1 to be string, array given 1Warning: strpos(): needle .

, , .

$needles=array('hose','fribb','pancake');
$haystack='Where are those pancakes??';

foreach($needles as $ndl){
   if(strpos($haystack,$ndl)!==false){ echo "'$ndl': found<br>\n"; }
   else{ echo "'$ndl' : not found<br>\n"; }
}

... , .

$haystack='Where are those pancakes??';
$match=preg_match('#(hose|fribb|pancake)#',$haystack);
//$match is now int(1)

, preg_match_all, , , total.

$all_matches=preg_match_all('#(hose|fribb|pancake)#',$haystack,$results);
//all_matches is int(2). Note you also have $results, which stores which needles matched.

. () , | "". # . , , , ! , , , . , .

+13

strstr()/stristr() ( )

if(strstr($string,$find)!==false){
    //true
} 
+3

strpos() .

if(strpos($string, $find) !== false)
. . .

, 0, -.

+3

All Articles