Jan 26, 2005 | PHP Scripting | By Shawn Parker
Regular expressions have always been (and will most likely continue to be) a challenge to construct. This site is using a system for parsing bb code in the posts that I swore to generate myself and am only permitting myself to seek help on it when I’m absolutely stuck. I guess that’s why it is taking so damn long.
But there was a breakthrough of sorts yesterday. I got the URL parsing to work correctly with the help of Google. The only bad part, and partly embarrassing, is that I’m not completely sure how it works. I went in search of help and found a few examples of people doing similar things and pretty much read their code to see what they were doing. Then I came across something that ended up helping. So, now, my URL parsing looks like this:
<?php // replace URLs $text = eregi_replace("\[url=([^\*)\]([^\*)\[/url\]","<a href=\"\1\">\2</a>", $text);
It works beautifully, and making a variant to insert off site links that open in a new window was terribly easy. What I’m not sure of is the use of the double escape characters. So, I’m gonna have to break out the Regex manual and figure out just why it works.
Other parts of the regex, like replacing bold, italic, and bold italic text and line breaks with the proper formatting, were easy.
<?php // replace , <b>,</b>,<i> & </i> tags $text = eregi_replace("\[(/?i|/?b)\]", "<\1>", $text); $text = eregi_replace("\[br\]", "", $text);
(yes, I’m still using
<i> tags – get over it!)
I’m digging Regex as a tool but man is it one hell of a brain buster. Every time I write some Regex and think I’m getting a hold of it I end up not having to do it for a while and then feel like I have to start all over again when I have to do something else complex.
Don’t get me wrong, I am proud of what I’ve been able to learn. Sometimes I wonder if I am capable of learning new things and this proves that this old dog can still hunt. This entry is a prime example – that I was able to figure that out makes me feel good about myself and that my brain is still capable of grasping complex concepts – even if that complex concept is just an unordered list.
Well, now that I’ve looked at the entry above I realize that I haven’t formatted blockquotes since I redesigned this sucker, so I’d better get on that before anyone actually reads this…
Tagged as: php, scripting, regex, regular-expressions