I have a group of cities and I want to see if a certain string contains a city from one in the group, if it does it will echo Yes. I was thinking to write all cities in a string, separated by commas.
$cities = "'Zimbabue', 'France', 'Sao Paulo'";
How can this be achieved ? if not separated by commas, with something else.
Edit=
strpos cant be used, if the string contaning all cities contains "São Paulo" and I try to find Paulo, it will output true but should be false
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
http://php.net/manual/en/function.in-array.php
You can combine the in_array and explode functions
echo ((in_array($searchTerm, explode(",", $cities)))?"Yes":"No");
or if you want a more readable version
$resultArray = explode(",", $cities);
$result = (in_array($searchTerm, $resultArray);
if ($result) {
echo "Yes"
} else {
echo "No";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With