2015-11-03 54 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); 
相關問題