I have a string, something like Paris (CDG) London (LHR)
I need to get the two values in brackets out and use them separately, like $Va1 and $Val2. Obviously as the text before is a city name the length will vary.
Preg Match somehow?
I have a string, something like Paris (CDG) London (LHR)
I need to get the two values in brackets out and use them separately, like $Va1 and $Val2. Obviously as the text before is a city name the length will vary.
Preg Match somehow?
Are these city codes predetermined, and if not, do they always consist of three uppercase English letters in parentheses preceded by a space that is preceded by letters? Are there always exactly two of them in the input?
Barring any further restrictions/requirements as laserlight has asked, you can grab any parenthesized string with something like...
php > $regex = '/(?<=\()[^\)]+(?=\))/';
php > $text = 'asdb (ABC) fdafd (123) Foo (Bar)';
php > preg_match_all($regex, $text, $matches);
php > print_r($matches);
Array
(
[0] => Array
(
[0] => ABC
[1] => 123
[2] => Bar
)
)
php >
See https://www.php.net/manual/en/regexp.reference.assertions.php for info on the look-ahead/-behind assertions I used.