All I want to do is something simple. I want to find all links in the text and replace them with a word. Say I have a text that reads: Hello everyone, this site is google: www.google.com and this site is yahoo!: http://yahoo.com. I rock. Code (markup): That should become: Hello everyone, this site is google: LINK and this site is yahoo!: LINK I rock. Code (markup): I currently have: preg_replace('~(http://|ftp://|https://|www\.)(.*)~i','LINK',$message) PHP: Cheers, Josh
hmm.. the (.*) will match the rest of the message because you didn't say what it should match up to.. try replacing it with (.*? ) this should match to the first space after the url preg_replace('~(http://|ftp://|https://|www\.)(.*? )~i','LINK',$message) PHP:
While it seems to work, for this string: www.google.com Or https://meh.com Code (markup): It replaces the entire string...because nothing is in there to do newlines. It works fine if you stick a space at the end of the line though
Not sure if I understand you properly but to match new lines you can put an s at the end of the regex preg_replace('~(http://|ftp://|https://|www\.)(.*? )~si','LINK',$message) PHP:
Nope. I currently have: preg_replace('~(http://|ftp://|https://|www\.?)(.*)([ \t\r\n\v\f]?)~is', 'LINK$3' , $message); PHP: To match all links, and against this string: www.google.com Or https://meh.com Code (markup): Or anything similar, it replaces the ENTIRE string with "LINK". Thank you.