I've never got my head around preg_replace formulas
I have a list of airport codes IATA and ICAO format inside a (), for example "(LHR / EGLL)"
How can I extract just the IATA code which is the first 3 letter code?
I've never got my head around preg_replace formulas
I have a list of airport codes IATA and ICAO format inside a (), for example "(LHR / EGLL)"
How can I extract just the IATA code which is the first 3 letter code?
One possibility:
$ php -a
Interactive shell
php > $regex = '#\(\s*([a-zA-Z]{3})\s*/[^\)]+\)#';
php > $string = "(LHR / EGLL)";
php > preg_match_all($regex, $string, $matches);
php > print_r($matches);
Array
(
[0] => Array
(
[0] => (LHR / EGLL)
)
[1] => Array
(
[0] => LHR
)
)
php > echo $matches[1][0];
LHR
Hmm, dunno about that. I don't know if you have to set something?
Python 3.6.8 (default, Apr 11 2019, 01:12:23)
[GCC 4.2.1 Compatible FreeBSD Clang 6.0.0 (tags/RELEASE_600/final 326565)] on freebsd11
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = 1
>>> if foo > 0:
... print "Foo has value"
File "<stdin>", line 2
print "Foo has value"
^
IndentationError: expected an indented block
I guess maybe it hinted, but didn't actually do it?
NZ_Kiwis Can you explain the regex to me?
# regex delimiter (typically '/', but can be many special chars)
\( literal opening paren (escaped because of regex meaning)
\s* 0-n white-space chars ('*' is the 0-n repetition indicator)
( start of capturing sub-pattern (shows up separately in $matches)
[a-zA-Z] any letter
{3} match exactly 3 of the preceding thing (any letter)
) end of sub-pattern
\s* 0-n white-space chars
/ literal slash (that's why I didn't use it as the regex delimiter)
[^\)]+ 1-n characters that are not a closing paren ('+' is the 1-n indicator)
\) a closing paren (escaped because of regex meaning)
# regex delimiter
Not sure this would work (because haven't seen all data; it works with your example), but you might also try:
$iata = trim(strtok($string, "/"), "( ");