当前位置:Gxlcms > PHP教程 > PHP如何快速找出字符串中匹配的字符(不能用正则)

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

时间:2021-07-01 10:21:17 帮助过:26人阅读

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] =>人中国
)

人气教程排行