View Full Version : Auto Parsing Links
boogyman
04-08-2007, 08:30 PM
How would I go about parsing links from an input when the link is contained within some other text.
mburt
04-08-2007, 11:29 PM
You mean:
<form method="post" action="?send">
<b>URL</b>:
<input type="text" name="url">
<br><input type="submit" value="Enter">
<hr>
<?php
$s = $_GET["send"];
if (isset($s)) {
echo "<a href=\"".$_POST["url"]."\">My link</a>";
}
?>
</form>
boogyman
04-09-2007, 03:10 AM
yes and no I understand how to parse the link once its recognized, im asking about the input being more then just a link... eg a comments box, but if they enter in http:// or www. then its going to be a link...
if(preg_match(/[(http://) | (www.)/) {
echo "<a href="MATCH + REST OF LINK">FULL URL</a>";
}
That would print out http:// or www. if thats printed, but what about the rest of the link ... and also ... i want it to search the full text entry, not just one line
difference between entering a url and a comment?
but i want to parse all urls that are given inside the comment part
mburt
04-09-2007, 12:43 PM
Oh I see. I use this on my wiki site:
function url_links($string){
//"www."
$pattern_preg1 = '#(^|\s)(www|WWW)\.([^\s<>\.]+)\.([^\s\n<>]+)#sm';
$replace_preg1 = '\\1<a href="http://\\2.\\3.\\4" target="_blank">\\2.\\3.\\4</a>';
//"http://"
$pattern_preg2 = '#(^|[^\"=\]]{1})(http|HTTP|ftp)(s|S)?://([^\s<>\.]+)\.([^\s<>]+)#sm';
$replace_preg2 = '\\1<a href="\\2\\3://\\4.\\5" target="_blank">\\2\\3://\\4.\\5</a>';
$string = preg_replace($pattern_preg1, $replace_preg1, $string);
$string = preg_replace($pattern_preg2, $replace_preg2, $string);
return $string;
}
boogyman
04-09-2007, 12:47 PM
(http|HTTP|ftp)(s|S)?://
I understand that, but can you explain the rest of the reg expression?
mburt
04-09-2007, 12:54 PM
I hardly understand myself, I got it from PHP.net... hehe.
I might be able to break it down though:
#(^|\s)(www|WWW)\.([^\s<>\.]+)\.([^\s\n<>]+)#sm
Any number of characters between \. and \. (which are just dots "."). The last segment, caries with the .extension.
The other regex's are just implentations of that one expanded a bit.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.