我正在尝试从这个字符串中获取一个匹配项
"Dial [Toll Free 1800 102 8880 ext: 246] to connect to the restaurant. <a class='tooltip' title='Foodiebay has now introduced value added calling features through the website. You just need to dial this number and we ..."
我想检查变量是否以字符串 Dial 开头的地方
$a = 'Dial [Toll Free 1800 102 8880 ext: 246] to connect to the restaurant. <a class='tooltip' title='Foodiebay has now introduced value added calling features through the website. You just need to dial this number and we';
preg_match('/[^Dial]/', $a, $matches);
最佳答案
去掉方括号:
/^Dial /
这与行首的字符串 "Dial "
匹配。
仅供引用:您的原始正则表达式是一个反转字符类 [^...]
,它匹配任何不在该类中的字符。在这种情况下,它将匹配任何不是“D”、“i”、“a”或“l”的字符。由于几乎每一行都至少有不是其中之一的字符,因此几乎每一行都会匹配。
关于php - 匹配以以下开头的内容的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4295249/