Searching for a better solution for parsing links than what I have now:

As many of you know by now, I’m developing an ajax driven chat room. I’m currently working out how to go about parsing posted links. there are 2 different formats, depending on whether or not someone wants to supply a title. Examples:

Dave: Check out my new web page at [link][/link].

Bill: Oh, yeah? I think [link=]Joe Cartoon's Site[/link] is really cool, too!

The code I’m currently using is as follows:

  function parseLinks($msg) {
    global $lastFunction;
    $lastFunction .= "_parseLinks";
    if ((strpos($msg, "[link") === false) or ($_SESSION['linkFilter'] == "true")) return $msg;
    $oldMsg = $msg;
    $search1 = "[link]";
    $search1a = "[link=";
    $search1b = "]";
    $search2 = "[/link]";
    if (strpos($msg, $search1) !== false) {
      $replStart = strpos($msg, $search1) + strlen($search1);
      $replEnd   = strpos($msg, $search2);
      $replLen   = $replEnd - $replStart;
      $URL  = substr($msg, $replStart, $replLen);
      $msg1 = substr($msg,0,$replStart - strlen($search1));
      $msg2 = substr($msg,$replEnd + strlen($search2));
      $title = $URL;
      $msg  = $msg1 . '<a target="_blank" href="'.$URL.'">'.$title.'</a>'.$msg2;
    else if (strpos($msg, $search1a) !== false) {
      $replStart = strpos($msg, $search1a) + strlen($search1a);
      $replEnd   = strpos($msg, $search1b);
      $replLen   = $replEnd - $replStart;
      $replTagEnd = strpos($msg, $search2);
      $replTagLen = $replTagEnd - ($replEnd + 1);
      $URL  = substr($msg, $replStart, $replLen);
      $title = substr($msg, $replEnd + 1, $replTagLen);
      $msg1 = substr($msg,0,$replStart - strlen($search1a));
      $msg2 = substr($msg,$replTagEnd + strlen($search2));
      $msg  = $msg1 . '<a target="_blank" href="'.$URL.'">'.$title.'</a>'.$msg2;
    return $msg;
// end function  parseLinks

I’m certain there’s a better way to do this using regular expressions, and I had thought the RegEx I saw earlier today seemed like it might have worked, but alas, no. What I have here seems to work, but there’s an awful lot of coding there to accomplish the job. I’m looking to streamline it, if possible. Any suggestions/tips/pointers? :slight_smile:

How about this

$rules = array(
$replace = array(
	'<a href="$1">$2</a>',
	'<a href="$1">$1</a>'

$text = 'blah blah [link][/link] blah blah blah [link=]google[/link] blah blah';

$msg = preg_replace($rules, $replace, $text);

We use this…

		$html = preg_replace("#\\[url\\](.*?)\\[/url\\]#is", "<a href=\\"$1\\" target='_blank'>$1</a>", $html);
		$html = preg_replace("#\\[url\\s*=\\s*(?:\\&quot\\;|\\")\\s*(.*?)\\s*(?:\\&quot\\;|\\")\\s*\\](.*?)\\[/url\\]#is", "<a href=\\"$1\\" target='_blank'>$2</a>", $html);
		$html = preg_replace("#\\[url\\s*=\\s*(.*?)\\s*\\](.*?)\\[/url\\]#is", "<a href=\\"$1\\" target='_blank'>$2</a>", $html);
		$html = preg_replace("#\\[url\\s*=\\s*(.*?)\\s*\\]#is", "<a href=\\"$1\\" target='_blank'>$1</a>", $html);

It covers all bbcode combinations

Hash, Webnet, thank you. I’ll try them both out, and see how it goes. I had thought about using .* , but thought it was “too simple” a solution. Over-thinking things is one of my biggest downfalls, without a doubt. :slight_smile:

Well, of the two options, I liked hash’s best, so not only did I implement it for the links, I also did it for the images, as well. :smiley:
VERY cool! and it took out about 30 lines of code, altogether. Again, thanks to you both.

Webnet has two extra rules you may want, aside from that it’s mostly just using arrays over multiple preg_replace.

More info on the .* here

If you want to play around this is nice.

WOW!! This one rules thanks I will never have problems with my regex from now on thanks

@hash and webnet
It’s probably not a bad idea to include them The code I’m using isn’t true BBCode, since it’s only got a few elements that are identical, and fewer still that are similar; but I’ll try to make it as close as I can, to prevent confusion. I’ve made a lot of progress on the overall function of the script, and if anyone wishes to give it a test drive, it can be found at:

Feedback is always welcome, and if you have questions or experience a problem, just let me know.

functionality wise, it works great! design could be uh… opinionated though.

Just my 2 cent, I’ve had some issues using AJAXed power chat on chatters who has crappy computer or slow connections. Depending on how active the chat is…it ends up not updating and even crashes the browser. Problem is that since it’s AJAX, you’re constantly pulling the message from the server every 2~5 sec? yours may vary but when your chat gets complexed enough to have PM’s, able to join multiple rooms in 1 screen, and etc… you’ll soon lose in the chaos of pulling the request. But, then again there could be magic silver bullet to do this in AJAX as well. G’luck! and uh… I think a designer should have a second glance though.

Thanks for having a look, sg707. I know the design aspect makes for a rather spartan page, but that’s just a simple matter of re-working the CSS once I’m finished with the code. and the way the page updates is through a small function that checks the row count in the database for the room the user is in. If the row count is the same (i.e no new messages) then the page isn’t updated. I’m trying to keep the bandwidth footprint as small as possible, since the combination of both chat room traffic AND ajax tends to create huge volumes of HTTP requests.

thats all great try and functional…lets try it all

I’m currently having “issues” with my hosting provider switching me from PHP 4 to PHP 5. I’ll have the problem fixed as soon as I get these “issues” fixed. Shouldn’t take more than a few hours (days, weeks, whatever) :smiley:

Ok, it’s back up and running. Chnaging from PEAR DB to PDO was a bit more “fun” than I had anticipated.:goof:

Thanks for sharing some codes… This will be helpful for all of us here… I’m a newbie on this and finding some tutorials…