我希望我的问题措辞正确!如果某个国家/地区的名称存在于字符串中,我想检索它。
$bio = 'A biography about someone from France';
$countries = ['Germany', 'Spain', 'France'];
如何使用国家数组检查字符串中是否存在国家?然后,如果找到匹配项,返回它?在这个例子中,我会留下 France 这个词。
试试这个代码:
print_r(array_intersect(explode(' ', $bio), $countries));
或者
foreach ($countries as $v)
if (mb_stripos($bio, $v) !== false)
{
echo $v;
break;
}
或者
echo @array_shift(array_intersect(explode(' ', $bio), $countries));
u have to use foreach on ur countries array, and inside check if strpos(bio, country) to find the position of the country word. if the position is not false it means it's there!!