识别并显示字符串中的禁止字符
我试图找到在 PHP 中执行此操作的最佳方法:
我必须分析字符串。
有些字符是禁止的(即:逗号、分号、空格、百分比...但它可以是我想要的任何字符,而不仅仅是标点符号!)
我想为字符串中的每个禁止字符打印一行:
$string = "My taylor, is rich%";
分析后,我想打印:
Character COMMA is forbidden
Character PERCENTAGE is forbidden
对于具有相同字符的多个错误,总和可能只有一行。
你们中有人遇到过这样的问题吗?
我尝试过 REGEX 和 STRPOS,但没有显着的结果。
I'm trying to find the best way to do this in PHP :
I have to analyse a string.
Some characters are forbidden (i.e. : comma, semicomma, space, percentage... but it can be any character I want, not only punctuation signs !)
I would like to print a line FOR EACH forbidden character in the string :
$string = "My taylor, is rich%";
After analyse, I want to print :
Character COMMA is forbidden
Character PERCENTAGE is forbidden
The summum could be to have only one line for multiple errors with the same character.
Did some of you experienced such a problem ?
I've tried REGEX and STRPOS, but without significant result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 preg_match_all。
上面的输出是:
preg_match_all
将返回$forbidden
正则表达式的所有实例。您可以根据需要调整该正则表达式中的字符。 array_unique 将消除重复项。最后,我只是在输出中输出字符本身。如果您想要“COMMA”、“PERCENTAGE”等单词,您将必须为其创建一个散列。
Use preg_match_all.
The output of the above is:
The
preg_match_all
will return all instances of the$forbidden
regex. You can adjust the characters in that regex as you see fit. Thearray_unique
will eliminate duplicates.Finally, I am just outputting the characters themselves in the output. If you want words like "COMMA", "PERCENTAGE", etc..., you will have to create a hash for that.