tags:

views:

73

answers:

2

I am looking to implement a system to strip out url's from text posted by a user.

I know there is no perfect solution and users will still attempt things like:

www dot google dot com

so I know that ultimately any solution will be flawed in some way... all I am looking to do really is reduce the number of people doing it.

Any suggestions, source or approaches appriciated,

Thanks

A: 

You need to define exactly what you want to strip out. The stricter the definition, the more false positives you will get. The following example will remove any string with 3 characters, followed by a period, more letters, another period and 2-4 more letters:

$text = preg_replace('/[a-z]{3}\.[a-z]+\.[a-z]{2,4}/i', '', $text);

The other end of strictness might be anything that ends on a period and 2-4 letters (like .com):

$text = preg_replace('/[a-z]+\.[a-z]{2,4}/i', '', $text);

Note that the latter will strip out the last word of a sentence, the full stop and the first word of the next sentence if someone forgets to add a space inbetween the sentences.

soulmerge
Hmm not great if it happened to be content related to programming (OP to confirm or not), as you'd likely end up getting rid of namespaces, package hierarchies and so on...
Wim Hollebrandse
its not related to programming... but thanks
Mark
+1  A: 

There are number of regular expression pattern matchers here. Some of them are quite complex. I would suggest that running multiple ones may be a good idea.

Preet Sangha
Mark
which you can test with this link if anyone else is looking for such a regular expressionhttp://www.regexlib.com/RETester.aspx?regexp_id=1016
Mark