1

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:

  • myCountry: region1, region2
  • myCountry: region1, region2, region3; otherCountry: region1
  • otherCountry: region1; myCountry: region1; otherCountry: region1, region2

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.

2
  • FYI: You are not using regular expressions in your example. str_replace does not use REGEX - preg_replace does php.net/manual/en/function.preg-replace.php Commented May 7, 2014 at 10:54
  • I know, just wanted to mention in case it is a better alternative here. Commented May 7, 2014 at 10:56

2 Answers 2

12

Using regular expression:

preg_match('/myCountry\:\s*([^\;]+)/', $mainString, $out);
$myRegion = $out[1];
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this ! One question: if there would be more countries after myCountry will this just go until the first semicolon after myCountry (which is what I need) ?
Yeah. It just takes everything from myCountry: to the first ;
The colon and semicolon do not require escaping.
5

Since from the comments it seems like you are interested in non-regex solutions and since you are a beginner and interested in learning, here's another possible approach using explode. (Hope this is not uncalled for).

Firstly, recognise that you have definitions seperated by ; since it is:

myCountry: region1, region2, region3 ; otherCountry: region1

So, using explode, you can generate an array of your definitions:

$string = 'otherCountry: region1; myCountry: region1; otherCountry: region2, region3';
$definitions = explode (';', $string);

giving you

array(3) {
  [0]=>
  string(21) "otherCountry: region1"
  [1]=>
  string(19) " myCountry: region1"
  [2]=>
  string(31) " otherCountry: region2, region3"
}

You could now iterate over this array (using foreach) and explode it using : and then explode the second result of that using ,. This way you could build up an associative array holding your Countries with their respective regions.

$result = array();
foreach ($definitions as $countryDefinition) {
  $parts = explode (':', $countryDefinition); // parting at the :
  $country = trim($parts[0]); // look up trim to understand this
  $regions = explode(',', $parts[1]); // exploding by the , to get the regions array
  if(!array_key_exists($country, $result)) { // check if the country is already defined in $result
    $result[$country] = array();
  }
  $result[$country] = array_merge($result[$country], $regions);
}

Just a very simple example to play with.

3 Comments

Thank you, I suggested an eddit for your array example because it's a little bit confusing. The array had 3 entries but you write only 2 entries. They match with your first sentence but not with your code part with explode.
@Sunchock I didn't see your edit but i think i fixed it?
I didn't see changes array(3) { [0]=> string(19) " myCountry: region1, region2, region3" [1]=> string(31) " otherCountry: region1" } would become array(3) { [0]=> string(19) "otherCountry: region1" [1]=> string(31) " myCountry: region1" [2]=> string(31) " otherCountry: region2, region3" } Because your array match with your first sentence but not with your example using explode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.