etrader Posted January 18, 2011 Share Posted January 18, 2011 I have a string containing long text. How can I remove links (in plain text, not hyperlink) starting with http://? In fact, I need a code to replace everything that is started with "http://" with "". Thanks Quote Link to comment https://forums.phpfreaks.com/topic/224838-removing-http-links-from-a-string/ Share on other sites More sharing options...
Anti-Moronic Posted January 18, 2011 Share Posted January 18, 2011 str_replace('http://', '') Quote Link to comment https://forums.phpfreaks.com/topic/224838-removing-http-links-from-a-string/#findComment-1161326 Share on other sites More sharing options...
etrader Posted January 18, 2011 Author Share Posted January 18, 2011 Thanks, but this replaces only "http://". I want to delete the whole phrase (the url). Quote Link to comment https://forums.phpfreaks.com/topic/224838-removing-http-links-from-a-string/#findComment-1161331 Share on other sites More sharing options...
BlueSkyIS Posted January 18, 2011 Share Posted January 18, 2011 given a URL without context, it's easy to tell where the url starts but not so easy to tell where it ends. is there some context for these URLs? Are they always "quoted" or anything? It would help a lot if we could see sample data. Quote Link to comment https://forums.phpfreaks.com/topic/224838-removing-http-links-from-a-string/#findComment-1161338 Share on other sites More sharing options...
etrader Posted January 18, 2011 Author Share Posted January 18, 2011 The end of each url is a space; as a matter of fact, we should consider the phrase between http:// and the next (first) space Quote Link to comment https://forums.phpfreaks.com/topic/224838-removing-http-links-from-a-string/#findComment-1161363 Share on other sites More sharing options...
BlueSkyIS Posted January 18, 2011 Share Posted January 18, 2011 this works with a simple test, but might need tweaked $content = "AS DGASDGAsa 3434 434SDGASDGADGhttp://www.cnn.com/something/somethingelse.html?whatever=4535&test=sagasdg MORE CONTENT heraes tq435345345q"; $pattern = '~http://[^\s]*~i'; $content = preg_replace($pattern, '', $content); echo "content: $content "; output: content: AS DGASDGAsa 3434 434SDGASDGADG MORE CONTENT heraes tq435345345q Quote Link to comment https://forums.phpfreaks.com/topic/224838-removing-http-links-from-a-string/#findComment-1161366 Share on other sites More sharing options...
cyberRobot Posted January 18, 2011 Share Posted January 18, 2011 You could remove the link based on the location of the http:// (or https://) and the location of the first space after the position of the http:// part. Note that this will fail if there is a space in the link. http://www.example.com/my page.html Quote Link to comment https://forums.phpfreaks.com/topic/224838-removing-http-links-from-a-string/#findComment-1161372 Share on other sites More sharing options...
etrader Posted January 18, 2011 Author Share Posted January 18, 2011 As far as I am testing, it is completely OK Thanks Quote Link to comment https://forums.phpfreaks.com/topic/224838-removing-http-links-from-a-string/#findComment-1161422 Share on other sites More sharing options...
.josh Posted January 18, 2011 Share Posted January 18, 2011 for good measure, you could do: $pattern = '~((ht|f)tps?|irc)://[^\s]*~i'; This will also match https, ftp and irc protocols. Quote Link to comment https://forums.phpfreaks.com/topic/224838-removing-http-links-from-a-string/#findComment-1161476 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.