Results 1 to 3 of 3
  1. #1

    [PHP] preg_replace URLs

    Hi there,

    I'm writing my own bbcode functions, and I'm pretty much done on that one. URL's are replaced if a user used [ url ][ /url ] or if a user used [ ]click here![ /url ]. Now comes the hard part: if a user didn't use that, and for example only typed www. google. be, I want to replace that as well. I tried to use the regex in the code box below, but that also replaces my already converted URL's...

    preg_replace("`(?<!<a href=)((http)+(s)?: (//)|(www\.))((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"http\\3://\\5\\6\\8\\9\" title=\"\\0\">\\5\\6</a>", $message);
    note: there is no space after "((http)+(s)?:", I added it because otherwise there will be a emoticon inserted.

    The output will then look something like this:
    <a href="<a href=" title=""></a> target="_blank">click here!</a>
    <a href="<a href=" title=""></a> target="_blank"><a href="</a>" title="</a>"></a>
    <a href="" title=""></a>

    Could someone be so kind to tell me what I'm doing wrong?

  2. #2
    Join Date
    Dec 2004
    There are many good ways to write parsers, but putting dozens of generalized regular expressions into your code is sure to be a slow and buggy solution, because you're constantly coding around all of these corner cases etc. My advice is to approach the problem from an entirely different direction. Write a proper parser; read each character in the input sequentially, and look for special input. If you see something interesting (say a [url] tag), switch your parser into a different context where it will react appropriately.

    This is not easy; I'd recommend taking a look at some of the existing code out there if you want to write a robust solution.

    As a solution to your actual problem, look into 'negative lookbehinds' in the Perl regular expression syntax. You want something like '(?<!\[url[=\]])' in front of your main pattern match.

  3. #3
    Thanks for your reply. I'm looking into negative lookbehinds right now.

    As for the regex you posted: I already have the [ url ] bbcode covered. I'm now looking into a way of automatically making urls clickable (say, if a user forgot to put the [ url ] bbcode in). So the html the regex needs to work on is the following:
    <a href="" target="_blank">click here!</a>
    <a href="" target="_blank"></a>
    Then it should *only* change the last url, and now it is changing all...

    I also downloaded "The Regex Coach", to try my regex, but it seems like I can't get negative lookbehinds to work in it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts