2017-02-16 51 views
1

我想比较一个字符串是否包含2个字符串。如果一个字符串包含2个字

这工作: -

$list_district = (strpos('1 bed flat in Sekaninova, Prague 2', 'Prague') !== false) ? yes: no ; 

这不是:

$list_district = (strpos('1 bed flat in Sekaninova, Prague 2', 'Prague 1') !== false) ? yes: no; 

感谢。

+1

嗨,你的目的是什么? –

+1

上面的代码是不正确的....你可以检查和更新 – Naincy

+2

[在这里工作正常](https://eval.in/738138).BTW你有一个TYPO错误在第二个代码 –

回答

1

您在留言问该做象下面这样: -

<?php 
$data = '1 bed flat in Sekaninova, Prague 2'; 
$districts = ["Prague 1","Prague 2"]; 

foreach ($districts as $district) { 
$district_array = explode(' ',$district); 

$district_string = "(".implode('|',$district_array).")"; 
if(preg_match("$district_string", $data) === 1) { 
    echo "yes"; 
    echo PHP_EOL; 
}else{ 
    echo "no"; 
} 

} 

输出: - https://eval.in/738216

+0

你是男人,我发现错误是它只在2件物品上循环。上述作品在所有地区......其中约25个。 非常感谢您的时间。 – Shina

+1

@Shina感谢您的赞赏。很高兴帮助你。:) :) –

0
if (strpos('1 bed flat in Sekaninova, Prague 2', 'Prague 2')) { 
    echo district; }; 

您也可以使用 “stripos函数” 是不区分大小写

相关问题