Jump to content

Deleting urls from a string


etrader

Recommended Posts

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"

:D

 

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.

$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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.