user1875332
user1875332

Reputation: 43

Adding Spaces to Numerals

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'];

Upvotes: 0

Views: 166

Answers (3)

eklundchristopher
eklundchristopher

Reputation: 159

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

Upvotes: 0

spinon
spinon

Reputation: 10847

for ($i = 2; $i <= 5; $i++)
{
    $val = $_GET["var" . $i];
    $pattern = '/\d/g';
    $replacement = '${1} ';
    $newVal = preg_replace(pattern, replacement, $val);
}

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212412

$var2 = implode(' ',str_split($_GET['var2']));

Upvotes: 3

Related Questions