如何检查我的数组是否包含特定单词

 $pattern = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";


 $str =  file_get_html($_GET['url'])->plaintext;


 if($num_found = preg_match_all($pattern, $str, $out)) {

    print_r($out[0]);

 }


 Output

 Array ([0] => https://ssl.gstatic.com/gb/images/b_8d5afc09.png [1] => https://ssl.gstatic.com/gb/images/b8_3615d64d.png [2])

因此,现在我想查找以下任何链接是否包含某些特定单词,例如“b_8d5afc09”,然后我想获取该链接并仅显示该链接。


到目前为止,我已经尝试过使用“in_array”,但即使链接存在,它也永远找不到,我做错了什么吗?任何建议表示赞赏!


if (in_array("b_8d5afc09", $out[0]))

  {

  echo "found";

  }

  else

  {

  echo "nope";

  }


慕容708150
浏览 106回答 1
1回答

慕森王

该函数in_array检查完整的针是否等于数组的一个值。不只是一部分。你可以strpos这样工作:foreach( $out[0] as $value )&nbsp; {&nbsp; if( strpos( $value, 'b_8d5afc09' ) !== false )&nbsp; {&nbsp; &nbsp; echo "found in $value";&nbsp; &nbsp; break;&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP