tags:

views:

46

answers:

3

I'm trying to add a space between any numbers that are passed over. Basically if $var2 was 1028 I want it to add a space so that it becomes 1 0 2 8. I only want it to add it for numerals though and not letters. I only need it to do it on var2-var5 and nothing above that. Any help is greatly appreciated! Thanks!

$apikey = $_GET['apikey'];
$campaign = $_GET['campaign'];
$phone = $_GET['number'];
$delay = $_GET['delay'];
$name = $_GET['var1'];
$var2 = $_GET['var2'];
$var3 = $_GET['var3'];
$var4 = $_GET['var4'];
$var5 = $_GET['var5'];
A: 

Here's a quick function which will make your life a lot easier.

function preg_add( $content, $regex, $replace = '${1}' ) {
   return trim( preg_replace( $content, $regex, $replace ) );
}

To use it you simply do; $var2 = preg_add( $var2, '/([0-9])/', '${1} ' );

Chris
You have an extra space at the end, though...
Matchu
+3  A: 
$var2 = implode(' ',str_split($_GET['var2']));
Mark Baker
We'd still have to check that `var2` is an integer, though.
Matchu
That would still need to be checked anyway, irrespective of the method used to split the characters.
Mark Baker
+1, very clever solution
Ryan Kinal
A: 
for ($i = 2; $i <= 5; $i++)
{
    $val = $_GET["var" . $i];
    $pattern = '/\d/g';
    $replacement = '${1} ';
    $newVal = preg_replace(pattern, replacement, $val);
}
spinon