Wednesday, August 20, 2008 - 09:53
The PHP function preg_replace() has powerful functionality in its own right, but extra depth can be added with the inclusion of the e modifier. Take the following bit of code, which just picks out the letters of a string and replaces them with the letter X.
1 2 3 | $something = 'df1gdf2gdf3sgdfg'; $something = preg_replace("/([a-z]*)/", "X", $something); echo $something; // XX1XX2XX3XX |
This is simple enough, but using the e modifier allows us to use PHP functions within the replace parameters. The following bit of code turns all letters upper case in a string of random letters by using the strtoupper() PHP function.
1 2 3 | $something = 'df1gdf2gdf3sgdfg'; $something = preg_replace("/([a-z]*)/e", "strtoupper('\\1')", $something); echo $something; // DF1GDF2GDF3SGDFG |
Here is another example, but in this case the full string is repeated after the modified string.
1 2 3 | $something = 'df1gdf2gdf3sgdfg'; $something = preg_replace("/([a-z0-9]*)/e", "strtoupper('\\1').'\\1'", $something); echo $something; // DF1GDF2GDF3SGDFGdf1gdf2gdf3sgdfg |
Notice that when using the e modifier it is important to properly escape the string with single and double quotes. This is because the string as a whole is parsed as PHP and so if you don't put single quotes around the backreferences then you will get PHP complaining about constants.
For a more complex example I modified the createTextLinks() function that wrote about recently on the site. The function originally found any URL strings within a larger string and turned them into links. The modified function now returns the same thing, except that the link text has been shortened using the shortenurl() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | $longurl = "there is the new site http://www.google.co.uk/search?aq=f&num=100&hl=en&client=firefox-a&channel=s&rls=org.mozilla%3Aen-US%3Aofficial"; function createShortTextLinks($str='') { if($str=='' or !preg_match('/(http|www\.|@)/im', $str)){ return $str; } // replace links: $str = preg_replace("/([ \t]|^)www\./im", "\\1http://www.", $str); $str = preg_replace("/([ \t]|^)ftp\./im", "\\1ftp://ftp.", $str); $str = preg_replace("/(https?:\/\/[^ )\r\n!]+)/eim", "'<a href=\"\\1\" title=\"\\1\">'.shortenurl('\\1').'</a>'", $str); $str = preg_replace("/(ftp:\/\/[^ )\r\n!]+)/eim", "'<a href=\"\\1\" title=\"\\1\">'.shortenurl('\\1').'</a>'", $str); $str = preg_replace("/([-a-z0-9_]+(\.[_a-z0-9-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)+))/eim", "'<a href=\"mailto:\\1\" title=\"Email \\1\">'.shortenurl('\\1').'</a>'", $str); $str = preg_replace("/(\&)/im","\\1amp;", $str); return $str; } function shortenurl($url){ if(strlen($url) > 45){ return substr($url, 0, 30)."[...]".substr($url, -15); }else{ return $url; } } echo createShortTextLinks($longurl); |
Comments
Submitted by Chris Schmidt (not verified) on Tue, 02/03/2009 - 18:31 Permalink
Submitted by Chris Schmidt (not verified) on Tue, 02/03/2009 - 17:48 Permalink
Submitted by Chris Schmidt (not verified) on Wed, 02/04/2009 - 10:24 Permalink
Submitted by Chris Schmidt (not verified) on Wed, 02/04/2009 - 10:28 Permalink
(?:<[^\\]a.*?>)(https?:\/\/[^ )<\r\n!]+)(?:<)If you are interested I use a tool called rework to test my regular expressions. Take a look - http://osteele.com/tools/rework/. In my experience, the easy part of writing regular expressions is matching things what you want, the difficult part is stopping it matching things you don't want.Submitted by Chris Schmidt (not verified) on Wed, 02/04/2009 - 11:19 Permalink
(<a.*?>)(https?:\/\/[^ )<\r\n!]+)(<)If you want to learn regular expressions quickly I can recommend getting Ben Forta's book Regular Expressions In 10 Minutes - ISBN 0672325667. I read that book and it all became clear, and it isn't as heavy going as some other books. I now use regular expressions every day and they don't scare me as much!Submitted by Chris Schmidt (not verified) on Wed, 02/04/2009 - 13:08 Permalink
Add new comment