开发者

Check is variable has this text

开发者 https://www.devze.com 2023-02-18 00:19 出处:网络
$check = array(\"this\", \"that\", \"to be\", \"not to be\"); $yes = $no = array(); I\'m trying to check, is each item of array $check contains text \"be\".
$check = array("this", "that", "to be", "not to be");

$yes = $no = array();

I'm trying to check, is each item of array $check contains text "be".

If it is, then add it to $yes, otherwise add t开发者_运维知识库o $no.

Seems I have to use a regular expression, can you please help me to compose it?


If you want to look for the complete word be and not be as a substring you can use:

$check = array("this", "that", "to be", "not to be");
$yes = $no = array();
foreach($check as $v) {
        if(preg_match('/\bbe\b/',$v)) {
                $yes[] = $v;
        } else {
                $no[] = $v;
        }
}


foreach($array as $arr)
{
   if(in_array('be', explode(' ', $arr)) == true)  
      $yes[] = $arr;
   else
      $no[] = $arr;
}


look at in_array()

0

精彩评论

暂无评论...
验证码 换一张
取 消