Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2012, 07:38 AM   PM User | #1
Jeru
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Jeru is an unknown quantity at this point
Regex: parse multiple URLS as links

I am trying to automatically parse URLS as a link in a text. It works with the script below. But the it only parses one link, not multiple. I am banging my head against the wall for hours now to solves this problem, can anyone help?


Code:
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

// The Text you want to filter for urls
$text = "This is my note with a link: https://domain.com/application.php?i=VHubsKu2ly7EfHXVLhEe# and with another link http://domain.com";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {

       // make the urls hyper links
       echo preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">link ></a>', $text);

} else {

       // if no urls in the text just return the text
       echo $text;

}
Jeru is offline   Reply With Quote
Old 02-12-2012, 09:06 AM   PM User | #2
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
You should use preg_match_all instead of preg_match.
gvre is offline   Reply With Quote
Old 02-12-2012, 09:16 AM   PM User | #3
Jeru
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Jeru is an unknown quantity at this point
Thanks Gvre for your reply.

I changed it to preg_match_all but it doesn't work as expected, the urls are not parsed properly.
Do you have any hints?

PHP Code:
$Note "This is my note with a link: https://domain.com/application.php?i=VHubsKu2ly7EfHXVLhEe# and with another link http://domain.com";


$reg_exUrl "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$stripped preg_match_all($reg_exUrl$Note$matchesPREG_SET_ORDER);

foreach (
$matches as $val) {
        
$Note preg_replace($reg_exUrl'<a href="'.$val[0].'">link </a>'$Note);

echo 
"$Note"
Jeru is offline   Reply With Quote
Old 02-12-2012, 01:14 PM   PM User | #4
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
You could use something like this

PHP Code:
$Note "This is my note with a link: https://domain.com/application.php?i=VHubsKu2ly7EfHXVLhEe# and with another link http://domain.com and this is another url http://www.foo.com/bar";

$pattern '#((?:http|ftp)s?://\S+)#i';
if (
preg_match_all($pattern$Note$matches))
{
        foreach (
$matches[1] as $val)
                
$Note str_replace($val"<a href=\"$val\">link</a>"$Note);
        echo 
$Note;

gvre is offline   Reply With Quote
Users who have thanked gvre for this post:
Jeru (02-12-2012)
Old 02-12-2012, 04:00 PM   PM User | #5
Jeru
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Jeru is an unknown quantity at this point
Yes, that's it!
Thanks gvre for your input, it's greatly appreciated!
Jeru is offline   Reply With Quote
Reply

Bookmarks

Tags
regex

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:41 PM.


Advertisement
Log in to turn off these ads.