php搜索以逗号分隔的字符串并获取匹配的元素

问题描述 投票:0回答:1

我有一个问题,是否有人可以帮助我解决这个问题。我有一个字符串,我想找到一个与搜索匹配的项目:

$search = "vox,eleciones,Gobierno";
$string = "El Gobierno de Pedro Sánchez ahonda en el trato de favor a País Vasco y Navarra";

$regex = '/([^,]*' . preg_quote($search, '/') . '[^,]*)/';

preg_match($regex, $string, $match);

$result = trim($match[1]);
print_r($result);

结果必须是:“ El Gobierno de PedroSánchezahonda en el trato de偏爱PaísVasco y Navarra”

php regex string filter preg-match
1个回答
0
投票

$ search =“ vox,eleciones,Gobierno”;$ string =“ El Gobierno de PedroSánchezahonda en el trato de偏爱PaísVasco y Navarra”;

$ search = explode(“,”,$ search);$ expression ='/ \ b(?:'。implode(“ |”,$ search)。')\ b / i';

            if(preg_match($expression, $string,$match)){

//成功}

© www.soinside.com 2019 - 2024. All rights reserved.