0

So, after some help from some lovely folks surfing stackoverflow, I got a regex to remove links that people posted. Now, I think I want to find one that removes their entire post, perhaps with " ", so my form will not allow the post. (instead of hey, check out my site at [LINK REMOVED]. Which is awesome, but could be better if it removed the whole sentence instead of just the link.) I am terrible with regexes atm, so any help would be greatly appreciated!

Here is my current regex:

$a = $_POST['msge'];
$b = preg_replace('%[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)%', '[LINK REMOVED]', $a);

Any ideas?

micker
  • 135
  • 2
  • 13

1 Answers1

1

There are better ways to find links in a string, here's an example in Perl that was given in this related question. If you're dead set on using a regex, this was mentioned in another related question and looks more promising than the one you're currently trying.

If you want to do replacement of the entire sentence given a link, you could use something like the following:

[^.|^!|^?]*(link)[^.|^!|^?]*[.|!|?]

Obviously you would want to replace link with your link pattern match.

Subjectively I would also suggest it may be a little odd to remove entire sentences from the middle of content that people are posting since it may alter the entire meaning of the post. If your main intent is to remove the link (for example, to prevent spam backlinks) you may just want to obfuscate the link by replacing it with something obvious like -LINK-.

Community
  • 1
  • 1
Tyson
  • 1,685
  • 15
  • 36