我想使用PHP代码获取字符串中的特殊字符,这是我的试用代码:
$page_num =">256";
if(preg_match('^[-<>]+$', $page_num))
{
//Here it should returns special chars present in string..
}
答案 0 :(得分:2)
检查这个
$page_num =">256";
if(preg_match_all('/\W/', $page_num,$matches))
{
print_r($matches);
}
答案 1 :(得分:1)
您错过的是 ending delimiter
$page_num =">256";
if(preg_match('^[-<>]+$^', $page_num))
^// this one
{
//Here it should returns special chars present in string..
}
演示代码here
或者你可以试试这个,
$string = 'your string here';
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $string))
{
echo "yes";
} else {
echo "no";
}
还有一个解决方案是
preg_match('![^a-z0-9]!i', $string);