etrader Posted February 9, 2011 Share Posted February 9, 2011 I have a string containing long text, which contains urls in the form of example.com or www.domain.com (simple plain text, not html links). How I can delete all of them. I need a php regex to delete all terms containing ".com" Quote Link to comment https://forums.phpfreaks.com/topic/227192-deleting-urls-from-a-string/ Share on other sites More sharing options...
jamesjmann Posted February 9, 2011 Share Posted February 9, 2011 I have a string containing long text, which contains urls in the form of example.com or www.domain.com (simple plain text, not html links). How I can delete all of them. I need a php regex to delete all terms containing ".com" you can copy and paste the code into microsoft word, then just use the "Find" tool to find all words with ".com" in it. Then copy and paste the code back into your text editing program. Quote Link to comment https://forums.phpfreaks.com/topic/227192-deleting-urls-from-a-string/#findComment-1171947 Share on other sites More sharing options...
etrader Posted February 9, 2011 Author Share Posted February 9, 2011 It is a variable string. It is not a static text. It comes from a dynamic array. Quote Link to comment https://forums.phpfreaks.com/topic/227192-deleting-urls-from-a-string/#findComment-1171956 Share on other sites More sharing options...
MadTechie Posted February 9, 2011 Share Posted February 9, 2011 $text = 'this is a test to remove URLs from text, the request was made by etrader on http://www.phpfreaks.com under the post http://www.phpfreaks.com/forums/php-coding-help/deleting-urls-from-a-string/?madeup=me Now the second one will not remove items like phpfreaks.com as it must start with http or www. or ftp. but the fire example looks for the ending like .com tests http://domain.com <--Removed http://www.domain.com <--Removed http://domain.com?test=123 <--Removed www.domain.com <--Removed'; //Option1 a- a simple example (your need to add to the domain ie .asia etc) //remove words ending with .com etc $newText= preg_replace('/\b[\S]*\.(?:co\.uk|com|net|org)\S*/i', '', $text ); //Option 2 (won't remove ones without either www. or http) //remove words starting with www. or http (and uses a RFC 3986 standard) $newText= preg_replace('/\b(?:https?:\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/i', '', $text); echo $newText; Quote Link to comment https://forums.phpfreaks.com/topic/227192-deleting-urls-from-a-string/#findComment-1171966 Share on other sites More sharing options...
MadTechie Posted February 9, 2011 Share Posted February 9, 2011 of course you could run them both but changing all the $newText to $text Quote Link to comment https://forums.phpfreaks.com/topic/227192-deleting-urls-from-a-string/#findComment-1171967 Share on other sites More sharing options...
etrader Posted February 9, 2011 Author Share Posted February 9, 2011 Actually, I loved the option1. It was so brilliant. Thanks a million Quote Link to comment https://forums.phpfreaks.com/topic/227192-deleting-urls-from-a-string/#findComment-1171969 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.