PHP头条
热点:

PHP如何快速找出字符串中匹配的字符(不能用正则)


PHP怎么快速找出字符串中匹配的字符(不能用正则)

$tags =  "中国,百度中国,人中国,漫画交流"; //固定数据格式

$tags_array = explode(',',$tag);

$match_result = array(); //匹配的结果

foreach($tags_array as $v){
      if(strpos($v,'中国') === false){
          continue; 
      }
      $match_result[] = $v;
}

//输出结果
Array
(
    [0] => 中国
     [1] => 百度中国
     [2] => 人中国
)



当然也可以用正则去匹配,我是想知道有没有更快的方法,谢谢大家! PHP 字符串 分享到:
------解决方案--------------------
这方法行不行?

$keyword = '中国';
$tags = ['中国', '百度中国', '人中国', '漫画交流'];

$match = array_filter($tags, function ($el) use ($keyword) {
  return (strpos($el, $keyword) !== FALSE);
});


Array
(
????[0] =>中国
????[1] =>百度中国
????[2] =>人中国
)

www.phpzy.comtrue/phprm/21421.htmlTechArticlePHP如何快速找出字符串中匹配的字符(不能用正则) PHP怎么快速找出字符串中匹配的字符(不能用正则) $tags="中国,百度中国,人中国,漫画交流";//固定数据格式 $tags_array=explode(',',$tag...

相关文章

PHP之友评论

今天推荐