PHP:检查从列表中特殊字符字符串中的present [英] PHP: Check if special characters from a list are present in a string

查看:155
本文介绍了PHP:检查从列表中特殊字符字符串中的present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个新手的问​​题。

This is a newbie question.

让我们说我有非法字符数组,即:

Let's say I have an array of illegal characters, i.e.:

$special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}");

我需要检查是否有这些字符是在字符串present,即

I would need to check if any of these characters is present in a string, i.e.

$my_string = "abcde!fgh"

我已经一派一个解决方案以简单的方式来做到这一点,但没有发现任何满意的答案。

I have googled for a solution to do this in a simple manner but haven't found any satisfactory answer.

任何帮助将是非常美联社preciated。

Any help on this would be much appreciated.

推荐答案

一个简洁的方式与你的两个数据结构做将是:

A concise way to do it with your two data structures would be:

count( array_intersect( str_split($my_string), $special_chars ) )

这也将告诉你有多少特殊字符的字符串中

That would also tell you how many of the special characters are in the string.

您本来写一个循环为你的角色列表和 strpos 手动探测。

You could otherwise write a loop for your character list and manually probe with strpos.

最少的努力将是你的特殊字符列表转换为正则表达式charclass将和测试对字符串。

The least effort would be converting your special character list into a regex charclass and testing against the string.

这篇关于PHP:检查从列表中特殊字符字符串中的present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆