Forum Moderators: coopster

Message Too Old, No Replies

Regular Expression to remove URLs

         

antriver

7:56 pm on Nov 2, 2009 (gmt 0)

10+ Year Member



Hi,
I'm trying to remove URLs from a string. I've got it to remove these formats of URLs:

google.com
www.google.com
http://www.google.com
http://google.com
http://example.google.com

$text = preg_replace("/(^¦\s)(http:\/\/)?(www\.)?[\.0-9a-zA-Z\-_~]+\.(com¦net¦org¦info¦name¦biz¦.+\.\w\w)((\/)?[0-9a-zA-Z\.\-_~#]+)?\b/ie","",$text);

The problem is that it is also taking out the plus symbol (+) from the text and anything written after an & gets removed.
e.g.

Testing + hello & blah
becomes
Testing hello
Thanks

antriver

8:52 pm on Nov 2, 2009 (gmt 0)

10+ Year Member



Never mind, I've solved the problem. It wasn't this, I needed to urlencode the text in jquery before sending the ajax request.