How to find elements in array that contain a given substring?

后端 未结 4 1993
礼貌的吻别
礼貌的吻别 2020-12-21 12:02

I have 3 strings, I would like to get only the equal strings of them, something like this:

$Var1 = \"Sant\";
$Array[] = \"Hello Santa Claus\";   // Name_1
$A         


        
4条回答
  •  梦毁少年i
    2020-12-21 12:39

    Your code will work too like below:-

    foreach ($Array as $name)
    {
        if (stristr($name,$Var1)!==false)
        {
            echo $name;
            echo PHP_EOL;
        }
    }
    

    Output:- https://eval.in/812376

    You can use php strpos() function also for this purpose

    foreach($Array as $name) 
    {
       if (  strpos($name,$Var1)!==false)
       {
         echo $name;
         echo PHP_EOL;
       }
    }
    

    Output:-https://eval.in/812371

    Note:- In Both function the first argument is the string in which you want to search the sub-string. And second argument is sub-string itself.

提交回复
热议问题