
常用函數strstr($ str,“ abc”);
正常比對preg_match(“ /(abc)?/ is”,$ str);
但是,如果要比對沒有特定字元串的字元串php 正則包含某字元串,則使用正規表達式會更麻煩
如果您不使用正常! strstr($ str,“ abc”);您可以解決問題
但是通過正則化,這是唯一的方法,“ / ^((?!abc). )* $ /是”
// --------------------------------------------- ---
複制代碼,代碼如下:
$ str =“ dfadfadf765577abc55fd”;
$ pattern_url =“ / ^((?!abc). )* $ /是”;
if(preg_match($ pattern_url,$ str)){
回顯“不包含abc!”;
}其他{
回顯“包含abc!”;
}
?>
// --------------------------------------------- ---
結果為: false,包含abc!
$ str =“ 2b3d5c”;
注意: [^(abc)]的文法是檢查$ str中的字元是否不在b c中,
preg_match(“ / [^(abc)] / s”,$ strphp 正則包含某字元串,$ arr);
字元2不在a b c中,是以$ arr的傳回值為2;
同時比對,包括字元串“ abc”,但不包括字元串“ xyz”
“ /(abc)[^((?!xyz). )* $] / s”
本文來自電腦雜談,轉載請注明本文網址:
http://www.pc-fly.com/a/ruanjian/article-209123-1.html