A little regex in php for fun

Ξ August 14th, 2008 | → 0 Comments | ∇ code, coding, php, programming, regex |

So after working thirteen hours in a row I came home and thought of a handy function to bandaid some legacy code that returns a url with double slashes after the TLD. I realize this is a worse solution than fixing the code in question, but my regex-fu could use work so I though I’d do a little exploring. Here’s what I came up with:

?php
$url = remove_url_slashes('http://www.killallhumans.com//?robots_eating=hobos&passion=361', 'killallhumans');

function remove_url_slashes($url=’’,$domain=’’) {
if(empty($url))
return false;
if(empty($domain))
$domain = 'killallhumans';
$pattern = "/($domain)(.*?)(\/+)/ie";
return preg_replace($pattern, "'\\1\\2/'", $url);
}
?

Original post by Maker

 

  • Rumours and Lies