Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

[PHP] LInk parsing

SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
Basically i am trying to parse "www.blah.com/efdsf" kinda thing in my forum script.

I have it work when an address has "http://" behind it but not when "www." is on it's own.

Here is my code:
PHP:
$post = preg_replace("`\b(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0" target="_blank">\0</a>', $post);

$post = preg_replace("/\s(www\.([a-z][a-z0-9_\..-]*[a-z]{2,6})([a-zA-Z0-9\/*-?&%]*))\s/i", " <a href=\"http://$1\">$1</a> ", $post);

Second one should parse with "www." on it's own but it doesn't.
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
liam_d said:
I tried that with this:
Code:
www.prxa.info/test
http://www.test.com
test.com
http://prxa.info

OK - I guess you have 2 preg_replaces running, both of which now match the http://www.text.com URL. I think you'll need to combine into a single one to do everything you want. Something like:

Code:
$post = preg_replace("/(http:\\/\\/)?((www\\.)?[a-z][a-z0-9_\\.-]*[a-z]{2,6}[a-zA-Z0-9\\/\\*-\\?&%]*)/i", '<a href="http://$2">$2</a>', $post);
 
0
Close
That is probably what is wrong as i was using a seperate one to check for http as well, i will try what you sent above this post and let you know how it goes :D
 
0
Close
Finally got around to testing and it is getting a lot closer:
Code:
www.prxa.info
/>www.prxa.com
/>www.prxa.info

and the html of it
Code:
<a href="http://www.prxa.info/test<br">www.prxa.info/test<br</a> /><a href="http://<a href="http://www.test.com">www.test.com</a>" target="_blank">http://<a href="http://www.test.com</a><br">www.test.com</a><br</a> />test.com<br /><a href="http://prxa.info" target="_blank">http://prxa.info</a>

Getting there, how do i fix this?
 
0
Close
It also now seems to try and parse most words into links :S
 
0
Close
Bump
 
0
Close
Ah edit. Just caught an error I'll work on it and see what I can do :)

-RageD
 
Last edited:
0
Close
ok, now I'm no regex expert by any stretch of the means, but this works without double parsing, etc.:

PHP:
        $post = preg_replace("`\b(https?|ftp|file)://[^www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0" target="_blank">\0</a>', $post);
	if(preg_match("/(www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*))/i", $post))
	{
		// Yes, order matters :P First one changes http://www.*
		// Second one just www.* to avoid the double parse
		$post = preg_replace("`\b(https?|ftp|file)://[www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0">\0</a>', $post);
		$post = preg_replace("(\b[^https?\\:\\/\\/|^ftp\\:\\/\\/|^file\\:\\/\\/](www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*)\b))", '<a href="http://$1">\0</a>', $post);
	}

-RageD
 
0
Close
Try:

Code:
$post = preg_replace("/(http:\\/\\/)?((www\\.)?[a-z][a-z0-9_\\.-]*\\.[a-z]{2,6}[a-zA-Z0-9\\/-\\.\\?&%]*)/i", '<a href="http://$2">$2</a>', $post);

this should avoid matching normal words (the expression must have a dot in it.)
 
Last edited:
0
Close
RageD said:
ok, now I'm no regex expert by any stretch of the means, but this works without double parsing, etc.:

PHP:
        $post = preg_replace("`\b(https?|ftp|file)://[^www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0" target="_blank">\0</a>', $post);
	if(preg_match("/(www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*))/i", $post))
	{
		// Yes, order matters :P First one changes http://www.*
		// Second one just www.* to avoid the double parse
		$post = preg_replace("`\b(https?|ftp|file)://[www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0">\0</a>', $post);
		$post = preg_replace("(\b[^https?\\:\\/\\/|^ftp\\:\\/\\/|^file\\:\\/\\/](www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*)\b))", '<a href="http://$1">\0</a>', $post);
	}

-RageD

That works but it won't turn urls with just a "www." into a link.

qbert220 said:
Try:

Code:
$post = preg_replace("/(http:\\/\\/)?((www\\.)?[a-z][a-z0-9_\\.-]*\\.[a-z]{2,6}[a-zA-Z0-9\\/-\\.\\?&%]*)/i", '<a href="http://$2">$2</a>', $post);

this should avoid matching normal words (the expression must have a dot in it.)

That errors me out with this:
Warning: preg_replace() [function.preg-replace]: Compilation failed: range out of order in character class at offset 65 in /home/prxainfo/public_html/forum/includes/post_parser.php on line 45

Warning: preg_replace() [function.preg-replace]: Compilation failed: range out of order in character class at offset 65 in /home/prxainfo/public_html/forum/includes/post_parser.php on line 45

Warning: preg_replace() [function.preg-replace]: Compilation failed: range out of order in character class at offset 65 in /home/prxainfo/public_html/forum/includes/post_parser.php on line 45

Thanks for all the help so far guys, can't wait to nail it on the head :)
 
Last edited:
0
Close
liam_d said:
That works but it won't turn urls with just a "www." into a link.

Doh! sorry, had an extra \b in there that shouldn't have been. Try this:

PHP:
$post = preg_replace("`\b(https?|ftp|file)://[^www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0" target="_blank">\0</a>', $post);
    if(preg_match("/(www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*))/i", $post))
    {
        // Yes, order matters :P First one changes http://www.*
        // Second one just www.* to avoid the double parse
        $post = preg_replace("`\b(https?|ftp|file)://[www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0">\0</a>', $post);
        $post = preg_replace("([^https?\\:\\/\\/|^ftp\\:\\/\\/|^file\\:\\/\\/](www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*)\b))", '<a href="http://$1">\0</a>', $post);
    }

Here is my exact test document:
PHP:
<?php
// main post parser
function main_post_parser($post)
{
    global $db;
    
    $post = htmlentities($post);
    
    $post = trim($post);
    
    // sort out new lines into breaks
    $post = str_replace(array("\r\n", "\r", "\n"), "<br />", $post);
    if( get_magic_quotes_gpc() )
    {
        $post = stripslashes($post);
    }

    if( !is_numeric($post) || $post[0] == '0' )
    {
        // $post = $db->escape($post); <-- Your version
	$post = mysql_escape_string($post); // I don't have the function :) lolz
    }
    
    // check if they are a guest, if they are check if guests links get parsed
    if ($_SESSION['group'] == 4)
    {
        if ($site_config['guest_links_parsed'] == 0)
        {
            // then don't parse
        }
        
        else
        {
            // auto-make links
            // Thanks to "geirha" from ubuntu forums for his amazing work on this for me :)
	   $post = preg_replace("`\b(https?|ftp|file)://[^www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0" target="_blank">\0</a>', $post);
    	   if(preg_match("/(www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*))/i", $post))
    	   {
      		  // Yes, order matters :P First one changes http://www.*
        	  // Second one just www.* to avoid the double parse
       		 $post = preg_replace("`\b(https?|ftp|file)://[www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0">\0</a>', $post);
       		 $post = preg_replace("([^https?\\:\\/\\/|^ftp\\:\\/\\/|^file\\:\\/\\/](www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*)\b))", '<a href="http://$1">\0</a>', $post);
    	   } 
        }
    }
    
    // they are not a guest so parse away
    else if ($_SESSION['group'] != 4)
    {
        // auto-make links
        // Thanks to "geirha" from ubuntu forums for his amazing work on this for me :)
	   $post = preg_replace("`\b(https?|ftp|file)://[^www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0" target="_blank">\0</a>', $post);
    	   if(preg_match("/(www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*))/i", $post))
    	   {
      		  // Yes, order matters :P First one changes http://www.*
        	  // Second one just www.* to avoid the double parse
       		 $post = preg_replace("`\b(https?|ftp|file)://[www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0">\0</a>', $post);
       		 $post = preg_replace("([^https?\\:\\/\\/|^ftp\\:\\/\\/|^file\\:\\/\\/](www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*)\b))", '<a href="http://$1">\0</a>', $post);
    	   }
    }
    return $post;
} 
 
echo main_post_parser("Check: http://test.com, check: www.test.com, check: http://www.test.com
Check line break.. w00t :)");

?>
 
Last edited:
0
Close
liam_d said:
That errors me out with this:

A dash in the wrong place. Try:

Code:
$post = preg_replace("/(http:\\/\\/)?((www\\.)?[a-z][a-z0-9_\\.-]*\\.[a-z]{2,6}[a-zA-Z0-9\\/\\.\\?&%-]*)/i", '<a href="http://$2">$2</a>', $post);
 
0
Close
RageD said:
Doh! sorry, had an extra \b in there that shouldn't have been. Try this:

PHP:
$post = preg_replace("`\b(https?|ftp|file)://[^www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0" target="_blank">\0</a>', $post);
    if(preg_match("/(www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*))/i", $post))
    {
        // Yes, order matters :P First one changes http://www.*
        // Second one just www.* to avoid the double parse
        $post = preg_replace("`\b(https?|ftp|file)://[www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0">\0</a>', $post);
        $post = preg_replace("([^https?\\:\\/\\/|^ftp\\:\\/\\/|^file\\:\\/\\/](www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*)\b))", '<a href="http://$1">\0</a>', $post);
    }

Here is my exact test document:
PHP:
<?php
// main post parser
function main_post_parser($post)
{
    global $db;
    
    $post = htmlentities($post);
    
    $post = trim($post);
    
    // sort out new lines into breaks
    $post = str_replace(array("\r\n", "\r", "\n"), "<br />", $post);
    if( get_magic_quotes_gpc() )
    {
        $post = stripslashes($post);
    }

    if( !is_numeric($post) || $post[0] == '0' )
    {
        // $post = $db->escape($post); <-- Your version
	$post = mysql_escape_string($post); // I don't have the function :) lolz
    }
    
    // check if they are a guest, if they are check if guests links get parsed
    if ($_SESSION['group'] == 4)
    {
        if ($site_config['guest_links_parsed'] == 0)
        {
            // then don't parse
        }
        
        else
        {
            // auto-make links
            // Thanks to "geirha" from ubuntu forums for his amazing work on this for me :)
	   $post = preg_replace("`\b(https?|ftp|file)://[^www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0" target="_blank">\0</a>', $post);
    	   if(preg_match("/(www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*))/i", $post))
    	   {
      		  // Yes, order matters :P First one changes http://www.*
        	  // Second one just www.* to avoid the double parse
       		 $post = preg_replace("`\b(https?|ftp|file)://[www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0">\0</a>', $post);
       		 $post = preg_replace("([^https?\\:\\/\\/|^ftp\\:\\/\\/|^file\\:\\/\\/](www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*)\b))", '<a href="http://$1">\0</a>', $post);
    	   } 
        }
    }
    
    // they are not a guest so parse away
    else if ($_SESSION['group'] != 4)
    {
        // auto-make links
        // Thanks to "geirha" from ubuntu forums for his amazing work on this for me :)
	   $post = preg_replace("`\b(https?|ftp|file)://[^www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0" target="_blank">\0</a>', $post);
    	   if(preg_match("/(www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*))/i", $post))
    	   {
      		  // Yes, order matters :P First one changes http://www.*
        	  // Second one just www.* to avoid the double parse
       		 $post = preg_replace("`\b(https?|ftp|file)://[www][-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]\b`", '<a href="\0">\0</a>', $post);
       		 $post = preg_replace("([^https?\\:\\/\\/|^ftp\\:\\/\\/|^file\\:\\/\\/](www\\.([a-z][a-z0-9_\\.-]*[a-z]{2,6})([a-zA-Z0-9\\/\\*-\\?&%]*)\b))", '<a href="http://$1">\0</a>', $post);
    	   }
    }
    return $post;
} 
 
echo main_post_parser("Check: http://test.com, check: www.test.com, check: http://www.test.com
Check line break.. w00t :)");

?>

That gave me this:

qbert220 said:
A dash in the wrong place. Try:

Code:
$post = preg_replace("/(http:\\/\\/)?((www\\.)?[a-z][a-z0-9_\\.-]*\\.[a-z]{2,6}[a-zA-Z0-9\\/\\.\\?&%-]*)/i", '<a href="http://$2">$2</a>', $post);

That worked perfectly, many thanks my man!
 
0
Close
Try using
Code:
strip_tags($post);
straight after defining the $post variable .
This should remove the unwanted page breaks etc
 
0
Close
qbert220 said:
A dash in the wrong place. Try:

Code:
$post = preg_replace("/(http:\\/\\/)?((www\\.)?[a-z][a-z0-9_\\.-]*\\.[a-z]{2,6}[a-zA-Z0-9\\/\\.\\?&%-]*)/i", '<a href="http://$2">$2</a>', $post);

I found a bug in this for some reason if i post this "test...test" it would turn that into a link that obviously goes no where?

http://forum.prxa.info/viewtopic.php?tid=2&fid=8

Take a look at the second post there.
 
0
Close
Bump
 
0
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back