2015-11-03 63 views
0

我想用PHP代码来获得在字符串中的特殊字符在字符串中找到特殊字符,这是我的审判代码:如何返回使用PHP

$page_num =">256"; 
if(preg_match('^[-<>]+$', $page_num)) 
{ 
     //Here it should returns special chars present in string.. 
} 
+0

是 “特殊字符” 总是在第一位置发现了什么?其他字符是否始终为数字? –

+0

不,它可能在字符串的任何地方像1-300,<200 – user3829086

+0

你错过了结尾分隔符。请检查我的答案下面的更正和其他方法。 –

回答

2

检查这个

$page_num =">256"; 
if(preg_match_all('/\W/', $page_num,$matches)) 
{ 
     print_r($matches); 
} 
+0

据打印空数组.. – user3829086

+0

其打印阵列 ( [0] =>数组 ( [0] =>> ) ) –

+2

[不看空到我(https://开头3v4l.org/GTJkC) –

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);