Jump to content

[SOLVED] How to replace 3 letter Airport Codes? Just the regex please.


phpdolan

Recommended Posts

Not clearly understanding any of the web tutorials, I've tried and failed at doing this on my own. Please read and offer.

 

I have a rather long string with mutliple instances of airport codes, ie ATL, BWI, LAX, etc. I want to replace them with a url (which I've got straight). They are always uppercase, and always flanked by ' (s)'. These space are important and used to distinguish when to replace. There are also only 2 other 3 letter caps that shouldn't be included: ODL and FLT.

 

Simple question. How is a regex formed to do this? I have the rest of the php syntax.

 

<mod> BTW What process would put the original 3 letter in a variable for use in the replace portion of the preg_replace? </mod>

 

Thanks,

David

Link to comment
Share on other sites

<pre>
<?php
$codes = array(
	'MDW' => 'Midway',
	'ORD' => "O'Hare",
);

function air_swap($matches) {
	global $codes;
	$code = $matches[0];
	if (array_key_exists($code, $codes)) {
		return $codes[$code];
	}
	else {
		return $code;
	}
}

echo $data = 'ODL  MDW  MDW XYZ ORD  FLT  AB1';
echo '<hr>';
echo preg_replace_callback('/[A-Z]{3}(?<!ODL|FLT)(?= )/', 'air_swap', $data);
?>
</pre>

Link to comment
Share on other sites

<pre>
<?php
$codes = array(
	'MDW' => 'Midway',
	'ORD' => "O'Hare",
);

function air_swap($matches) {
	global $codes;
	$code = $matches[0];
	if (array_key_exists($code, $codes)) {
		return $codes[$code];
	}
	else {
		return $code;
	}
}

echo $data = 'ODL  MDW  MDW XYZ ORD  FLT  AB1';
echo '<hr>';
echo preg_replace_callback('/[A-Z]{3}(?<!ODL|FLT)(?= )/', 'air_swap', $data);
?>
</pre>

 

OK effigy, I'll bite. Appreciate your reply. You kind of lost me in the code. I need to mull it over. The goal was to identify 'MDW' or 'ORD' and use them IN the replacement. So, the regex is clear, but your code seems like more than needed. The replacement will be '<a hre....... =KMDW>MDW /a>'. In other words, find the regex and replace using it. Suggestions?

 

Thanks,

David

Link to comment
Share on other sites

So the URLs are always the same with a simple exchange? In that case the following will work:

 

<pre>
<?php
echo $data = 'ODL  MDW  MDW XYZ ORD  FLT  AB1';
echo '<hr>';
echo preg_replace('/([A-Z]{3})(?<!ODL|FLT)(?= )/', '<a href="http://www.google.com/search?q=$1">$1</a>', $data);
?>
</pre>

Link to comment
Share on other sites

echo preg_replace('/([A-Z]{3})(?<!ODL|FLT)(?= )/', '<a href="http://www.google.com/search?q=$1">$1</a>', $data);

 

Effigy,

 

Thanks for the super reply. That worked for me, with one exception. It replaced several more items than needed. The airport identifiers to be changed are flanked by ' ', so I tried to add that with the code you used like so:

 

preg_replace('/(?= )([A-Z]{3})(?<!ODL|FLT)(?= )/', '...a href="http://www.google.com/search?q=$1"...$1\</a\>', $data);

 

and every iteration I could imagine. None worked. Guess I'm gonna have to buy the book?!  In the meantime, can someone please provide insight to how that really gets written and what are the '/' for at the beginning and end.

 

Thanks again,

David

Link to comment
Share on other sites

<pre>
echo preg_replace('/([A-Z]{3})(?<!ODL|FLT)(?= )/', '<a href="http://www.google.com/search?q=$1">$1</a>', $data);
</pre>

 

Update:

 

This code did find more than required uppercase 3 letter strings including: DPTR; DTIME BLOCK and HOTELS.

 

The deciding factor on what to replace is the surrounding ' ' for example

<pre>
<td valign=top class=SEQRowPrinter> [b]ORD[/b] </td>
</pre>

 

By adding the other (not required) 3 letters, ie PTR, IME, OCK and ELS like so: '(?<!ODL|FLT|IME|DAY|PTR|RVL|ELS|OCK|UTY)', the problem has been solved, but I'd sure like to know how to use the lookaround.

 

Also, what does the '/' at the beginning and end of the regex indicate?

 

Thanks for all your help,

?David

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.