Jump to content

preg_replace/regular expression confusion


clonetrooper9494

Recommended Posts

I was working a script to replace urls in a forum post in to html using preg_replace. This is my first experience using regular expression, but I thought I had it when I got it to work on this editor: http://rejex.heroku.com/ with the regex string being "(http://[\S]+)" and source being "this is a site http://website.com/ go there". So I stuck that in preg_replace and thought I would be done after that, but it isn't working...

<?php$input = 'this is a test post. http://website.com/yeah.php <- go there please!';echo "before:<br>".$input."<br>after:<br>";$output = preg_replace('(http://[\S]+)','<a href="$1">$1</a>',$input);echo $output;?>

returns

before:this is a test post. http://website.com/yeah.php <- go there please!after:this is a test post. <- go there please!

the html is being hidden because the variables aren't working, or something. The source of the page shows the HTML but no URL..can any one help?

Link to comment
Share on other sites

One or more of these might be what you're looking for:Automatically make active links to URLs. Works for both 'http:' and 'mailto:' links.http://psoug.org/snippet/PHP-Auto-Link-Maker_20.htmAnd a supposedly more secure version:More secure version of the link conversion codehttp://psoug.org/snippet/Secure-Auto-Link-Maker_21.htmAnd this one, which looks kind of gruesome to me:http://psoug.org/snippet/Auto-Link-Maker-2_130.htm

Link to comment
Share on other sites

It's probably using the parenthesis() as delimiters, so $1 wasn't being set.This possibly would have worked:preg_replace('#(http://[\S]+)#','<a href="$1">$1</a>',$input);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...