I just started using PHP and hope someone here can help me with this.
I am trying to extract a string ("myRegion
") from another string ("mainString
") where my string always starts with "myCountry:
" and ends with a semicolon (;
) if the main string contains more countries after myCountry OR without anything if the main string does not contain more countries after it.
Examples to show different options for main string:
What I want to extract is always the part in bold.
I was thinking of something like the following but this doesn't look right yet:
$myRegions = strstr($mainString, $myCountry);
$myRegions = str_replace($myCountry . ": ", "", $myRegions);
$myRegions = substr($myRegions, 0, strpos($myRegions, ";"));
Many thanks in advance for any help with this, Mike.
Using regular expression:
preg_match('/myCountry\:\s*([^\;]+)/', $mainString, $out);
$myRegion = $out[1];