需要var_dump帮助

时间:2010-04-06 21:47:34

标签: php

以下编码分隔$ state数据,我需要帮助将var_dump信息放入$ dat1 $ dat2和$ dat3(

if($en['country'] == 'US'){
   if ($_POST['locus'] == ''){$err .= 'Please select and fillin your locale.<br>';}
   $state = $_POST['locus']; // separate: NEW YORK, NY 10011
   preg_match("/(.*)\,\s+([A-Z]{2})\s+([\d]+)/", $state, $parts); 
   var_dump($parts);

} elseif($en['country'] == 'CA'){
   if ($_POST['locca'] == ''){$err .= 'Please select and fillin your locale.<br>';}
   $state = $_POST['locca']; // separate: PARADISE, NL A1L1P1
   preg_match("/(.*)\,\s+([A-Z]{2})\s+([\S]+)/", $state, $parts); 
   var_dump($parts);

} elseif($en['country'] == 'GB'){
   if ($_POST['locuk'] == ''){$err .= 'Please select and fillin your locale.<br>';}
   $state = $_POST['locuk']; // separate: LONDON, H9
   preg_match("/(.*)\,\s+([\S]{2})/", $state, $parts); 
   var_dump($parts); 
}

3 个答案:

答案 0 :(得分:2)

$ parts [0]将拥有匹配的整个字符串 $ parts [1],$ parts [2]和$ parts [3]将获得您想要的数据:

$dat1 = $parts[1];
$dat2 = $parts[2];
$dat3 = $parts[3];

答案 1 :(得分:1)

你可以做到

ob_start();
var_dump($parts);
$dat1 = ob_get_flush();

答案 2 :(得分:1)

为什么? var_dump不是这种方式使用的。它是纯粹的调试功能,这就是为什么它没有输出到变量。你应该使用其他功能。

使用变量:

$part[0] ... the whole expression of the reg. exp. is stored in this variable
$part[1] ... content of the first subexpression
$part[2] ... ...
$part[3]

您还应该避免代码中的冗余。 $ err变量可以在第一个条件之前设置。

相关问题