How to remove email addresses and links from a string in PHP?

后端 未结 5 886
终归单人心
终归单人心 2020-12-05 22:17

How do I remove all email addresses and links from a string and replace them with \"[removed]\"

5条回答
  •  难免孤独
    2020-12-05 22:52

    There are a lot of characters valid in the first local part of the email (see What characters are allowed in an email address?), so these lines would replace all valid email addresses:

    \1', $t);
    
    # replace urls:
    a='A-Za-z0-9\-_';
    $t = preg_replace("/[htpsftp]+[:\/\/]+[$a]+\.+[$a\.\/%&=\?]+/i", '[removed]', $t);
    

    This will cover most valid email addresses, be informed: removing really only all valid email addresses is a bit more complex (see How to validate an email address using a regular expression?)

提交回复
热议问题