Link Parser Oddity

SD_Ryoko

Ultra Cool Member
Reaction score
85
Remember this?

Code:
function Parse_Links($str)
{
   $AceHart = '#(http(s)?://)?(((?<=http)|[-\w0-9]+\.)([-\w0-9]+\.)+[a-z]+([/\?](\S*[^\s.,()?!:;]+)*)?)#i';
   $str=preg_replace($AceHart,'<a href="http\2://\3">\3</a>',$str);
   return $str;
}

Now, its acting a little funny.

The link is

'http://world-editor-tutorials.thehelper.net'

The link (href) is correct when displayed.

But it displays 'world-editor-tutorials.thehelper.net'

Without the http://

Also

I added IMG tag functionality.

So if you have a URL for an img inside the IMG tags, the link parser kills it, they fight over it.
Code:
$bodytext = preg_replace('/\[img\](.*?)\[\/img\]/is', '<img src=\'$1\'/>', $bodytext);

Here this is shown very well.
http://www.griffonrawl-ohio.com/grwbbs.php
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
> Remember this?

Looks like line noise to me.


> It displays ... without the http://

That's normal. And it did so since day one.


> IMG tags ... they fight over it.

That's no surprise either.
You wanted something that has a 99.9% or better chance of finding anything that might just be a link.
You got it.


Only run the link parser on the parts that are not IMG tags.
Split on IMG /IMG, run it on all the parts separately.
Carefully put it back together...

Sounds simpler than it is however... be prepared for some :banghead:
 

SD_Ryoko

Ultra Cool Member
Reaction score
85
Okay, its a little sloppy. I think better methods could be used for determining URL vs IMAGE, but this seems to work well.
PHP:
function Parse_Links($problem)
{
$AceHart = '#(http(s)?://)?(((?<=http)|[-\w0-9]+\.)([-\w0-9]+\.)+[a-z]+([/\?](\S*[^\s.,()?!:;]+)*)?)#i';
$Ryoko = "/\[img\](.*?)\[\/img\]/is";
$chunks = preg_split ($Ryoko, $problem, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$Final ="";
foreach($chunks as $key => $value)     // for each peice
{
	// If the chunk has a URL
	if (substr_count($value, "www.") > 0 || substr_count($value, "http") > 0 ) // Some sort of URL
		{
		// If the url looks like an image
		if (substr_count($value, ".jpg") > 0 || substr_count($value, ".gif") > 0 || substr_count($value, ".bmp") > 0 || substr_count($value, ".png") > 0 )
			// Then parse it as an image.
			$Final=$Final.preg_replace($AceHart,'<img src="http\2://\3">',$value);
		else
			// Else, parse it as a link.
			$Final=$Final.preg_replace($AceHart,'<a href="http\2://\3" target="there">\3</a>',$value);
		}
	else
 		$Final=$Final.$value;
}
return $Final;
}

The result:
http://www.griffonrawl-ohio.com/grwbbs.php
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top