acctman
acctman

Reputation: 4349

var_dump help needed

The coding below seperates the $state data, I need help putting the var_dump info into $dat1 $dat2 and $dat3 where needed

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

Upvotes: 0

Views: 214

Answers (3)

MartyIX
MartyIX

Reputation: 28686

What for? var_dump is not ment to be used this way. It is purely debugging function and this is why it doesn't have an output to a variable. You should use other functions.

Use variables:

$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]

You should also avoid of redundancy in your code. $err variable may be set in before the first condition.

Upvotes: 1

pocketfullofcheese
pocketfullofcheese

Reputation: 8857

$parts[0] will have the whole string that is matched $parts[1], $parts[2], and $parts[3] will have the data you want:

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

Upvotes: 2

Oliver Nagy
Oliver Nagy

Reputation: 336

You could do

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

Upvotes: 1

Related Questions