如果一个字符串包含2个单词

时间:2017-02-16 06:40:02

标签: php

我正在尝试比较字符串是否包含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;

感谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

此外,您可以使用“stripos”不区分大小写