PHP preg_match?如何返回不匹配的字符?
可以说我有:
$string = 'qwe1ASD@';
if(preg_match('/^[a-zA-Z]+$/', $string))
{
echo 'OK';
}
else
{
echo 'BAD';
}
现在,有没有简单的解决方案,可以找到 $string 中与表达式不匹配的所有字符?所以作为回报,我想要有前任来代替“坏”。 “不好。您不能使用以下字符:1@”
提前感谢您提供任何简单的提示! :)
谢谢 Floern,你的回答最适合我的需要。它只有一个“preg”,因此也有利于性能。再次感谢您。 我现在实现如下:
if(preg_match_all('/[^a-zA-Z0-9]/s', $string, $forbidden))
{
$forbidden = implode('', array_unique($forbidden[0]));
echo 'BAD. Your string contains forbidden characters: '.htmlentities($forbidden).'';
}
Lets say i have:
$string = 'qwe1ASD@';
if(preg_match('/^[a-zA-Z]+$/', $string))
{
echo 'OK';
}
else
{
echo 'BAD';
}
Now, is there simple solution, to find all characters from $string which don't match expression? So in return, in place of "BAD" i want to have ex. "BAD. You can't use following characters: 1@"
Thanks in advance for any simple hints! :)
Thank you Floern, your answer suit best my needs. It have only one "preg" so it's also good for performance. Thank you again.
I implemented it for now as follw:
if(preg_match_all('/[^a-zA-Z0-9]/s', $string, $forbidden))
{
$forbidden = implode('', array_unique($forbidden[0]));
echo 'BAD. Your string contains forbidden characters: '.htmlentities($forbidden).'';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
preg_match_all()
:You could use
preg_match_all()
:有不同的方法。我觉得这个不错:
更新:
There are different ways. I find this one nice:
UPDATE: