开发者

Preg replace - reg ex needed

开发者 https://www.devze.com 2023-01-02 02:54 出处:网络
Looking for a reg ex to null (empty) the string if it any contains the bad word.. $string1 = \"Ihatestackoverflow\";

Looking for a reg ex to null (empty) the string if it any contains the bad word..

    $string1 = "Ihatestackoverflow";
    $string2 = "I HaTe sackoverflow";
    $string3 = "1HaTestackoverflow";

    $badword = "hate";

# result
# string1 = "";
# string2 = "";
# strin开发者_开发知识库g3 = "";


$regex = '#^.*?(hate).*$#ims';
$str = preg_replace($regex, '', $str);

To add more "bad words", just add them in the ():

$regex = '#^.*?(hate|anotherbadword|yetanother|etc).*$#ims';

The leading ^ and trailing $ match the full string. The i option is for case insensitive matching. The m enables multi-line mode (so that ^ and $ match the whole string, as opposed to a single line in it). The s enables DOTALL (so the . matches new line characters as well).

You could

0

精彩评论

暂无评论...
验证码 换一张
取 消