Shina
Shina

Reputation: 2066

If a string contains 2 words

I'm trying to compare if a string contains 2 words string.

This works:-

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

This not:

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

Thanks.

Upvotes: 1

Views: 98

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72289

What you asked in comment for that do like below:-

<?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";
 } 

}

Output:-https://eval.in/738216

Upvotes: 1

Alexey Shatrov
Alexey Shatrov

Reputation: 470

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

Also you can use "stripos" to be case insensitive

Upvotes: 0

Related Questions