2017-04-23 56 views
-1

你们知道在这个preg_match中是否可以删除冒号“:”后的所有内容?PHP,preg_match去除冒号后的所有内容?

该代码基于旧数组创建一个新数组。 但价值是在这种格式45656412124:464565445,我只想要的第一部分的价值(45656412124)。

这是我今天该怎么办,我觉得有点笨:

$mods = []; 
    foreach($Query->GetRules() as $key => $val) 
     if(preg_match('/MOD\d+_s/ui', $key)) 
       $mods[$key] = $val; 

foreach($mods as $key => $val) { 
    $mods[$key] = strstr($val, ':', true); 
} 
+2

$ mods [$ key] =(explode(':',$ val))[0]; – 2017-04-23 01:02:15

回答

1

我讨厌的正则表达式,所以我使用这些简单分裂爆炸。简单而干净。

$string="45656412124:464565445"; 
$result_array=explode(":",$string); 
echo $result_array[0]; 
1

正则表达式:从起点/^\d+/这里这个表达式意味着只得到数字(\d+)。

解决方案1:Try this code snippet here

<?php 

ini_set('display_errors', 1); 
$string="45656412124:464565445"; 
preg_match("/^\d+/", $string,$matches); 
echo $matches[0]; 

解决方案2:Try this code snippet here

<?php 
$string="45656412124:464565445"; 
list($firstPart,$secondPart)= explode(":", $string); 
echo $firstPart; 
相关问题