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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.