[英]preg_match not working on long regular expressions
I want to match a keyword using preg_match in php. 我想在php中使用preg_match匹配关键字。 regular expression is working perfectly on www.regexr.com but not in my php code. 正则表达式可以在www.regexr.com上正常运行,但不能在我的php代码中使用。 can someone help. 有人可以帮忙。 Thankyou. 谢谢。
<?php
$regexx="/[sS]+([\s\t\r]*[\.\~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\[\]\{\}\;\:\"\'\\\|\,\.\<\>\/\?\d\w]*)+[hH]+([\s\t\r]*[\.\~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\[\]\{\}\;\:\"\'\\\|\,\.\<\>\/\?\d\w]*)+[aA]*([\s\t\r]*[\.\~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\[\]\{\}\;\:\"\'\\\|\,\.\<\>\/\?\d\w]*)+[rR]*([\s\t\r]*[\.\~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\[\]\{\}\;\:\"\'\\\|\,\.\<\>\/\?\d\w]*)+[eE]*
/";
if (preg_match($regexx, "S-hare"))
{
echo 'succeeded';
}
else
{
echo 'failed';
}
?>
Based on your comment: 根据您的评论:
I want to match the word 'share' written in any format. 我想以任何格式匹配“共享”一词。 for example.. any special character or space in between 'share' would be detected. 例如,..“共享”之间的任何特殊字符或空格都将被检测到。
I would suggest to simply remove every non alphabetics characters then match what is left. 我建议删除所有非字母字符,然后匹配剩余的字符。
var_dump($str = preg_replace("/[^a-z]/i", "", "sh@a/#~r: e !!!"));
var_dump($str === "share");
A raw single regex solution could be: 原始的单个正则表达式解决方案可以是:
/[^az]*s[^az]*h[^az]*a[^az]*r[^az]*e[^az]*/i
Which is simple share and [^az]*
to match any non alphabetics series of characters between every letters. 简单共享和[^az]*
可匹配每个字母之间的任何非字母系列字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.