phpdolan Posted July 21, 2008 Share Posted July 21, 2008 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted July 22, 2008 Share Posted July 22, 2008 <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> Quote Link to comment Share on other sites More sharing options...
phpdolan Posted July 22, 2008 Author Share Posted July 22, 2008 <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 Quote Link to comment Share on other sites More sharing options...
effigy Posted July 22, 2008 Share Posted July 22, 2008 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> Quote Link to comment Share on other sites More sharing options...
phpdolan Posted July 24, 2008 Author Share Posted July 24, 2008 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 Quote Link to comment Share on other sites More sharing options...
phpdolan Posted July 24, 2008 Author Share Posted July 24, 2008 <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 Quote Link to comment Share on other sites More sharing options...
effigy Posted July 24, 2008 Share Posted July 24, 2008 You need a lookbehind; try /(?<= )([A-Z]{3})(?<!ODL|FLT)(?= )/. The slashes are delimiters. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.