2015-12-03 15 views
-3

如何在php上使用正則表達式檢測數學運算符上的所有符號?如何在php上使用正則表達式檢測數學運算符上的所有符號?

例如:

$operators = [">0",">=1","==12","<9","<=1","!=4"]; 
$results = array(); 
foreach ($operators as $key => $value){ 
    detect $value using regex, if include symbol of maths operators { 
    array_push($results, $value); 
    // just push $value with format of symbol of maths operators, example : ">" // remove 0 on my string 
    } 
} 

從我的陣列我想收集只是數學運算符,我的預計業績:

$results = [">",">=","==","<","<=","!="]; 

如何做到這一點? 謝謝先進

+0

只是使用'array_map'和'preg_replace'功能.. – check

回答

1

您可以簡單地使用array_mappreg_replace等作爲

$operators = [">0", ">=1", "==12", "<9", "<=1", "!=4"]; 
print_r(array_map(function($v) { 
      return preg_replace('/[^\D]/', '', $v); 
     }, $operators)); 

輸出一起:

Array 
(
    [0] => > 
    [1] => >= 
    [2] => == 
    [3] => < 
    [4] => <= 
    [5] => != 
)