How would I go about parsing links from an input when the link is contained within some other text.
How would I go about parsing links from an input when the link is contained within some other text.
You mean:
Code:<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>
- Mike
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...
PHP Code: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
Oh I see. I use this on my wiki site:
Code: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; }
- Mike
I understand that, but can you explain the rest of the reg expression?(http|HTTP|ftp)(s|S)?://
I hardly understand myself, I got it from PHP.net... hehe.
I might be able to break it down though:
Any number of characters between \. and \. (which are just dots "."). The last segment, caries with the .extension.Code:#(^|\s)(www|WWW)\.([^\s<>\.]+)\.([^\s\n<>]+)#sm
The other regex's are just implentations of that one expanded a bit.
- Mike
Bookmarks