PDA

View Full Version : String manipulation on a (phone) number

Arnaud
10-21-2009, 04:38 PM
Hi Folks,

Quick question.

Say I have a variable that contains a (phone) number. Only digits, this is.

<?
\$number = 0123456789;
?>

I would like to convert that number in a sequence that would display:
012 345 67 89

Now if my number is 00112233445566 it should return:
0011 223 344 55 66

Any longer number, say 0001112233445566 should return:
000111 223 344 55 66

So, basically, is should display blocks of n numbers separated by spaces:

[any number of digits][space][3 digits][space][3 digits][space][2 digits][space][2 digits]

I am sure this is quite easy to do, but I can't figure out how... any help will be appreciated!

TIA

Fumigator
10-21-2009, 05:22 PM
Look into the substr() function.

http://us3.php.net/manual/en/function.substr.php

You'll also need to count the number of digits using strlen().

http://us3.php.net/manual/en/function.strlen.php

oesxyl
10-21-2009, 05:33 PM
<?php

// \$phone = '0001112233445566';
\$phone = '2233445566';
echo preg_replace("/(\d+)?(\d{3})(\d{3})(\d{2})(\d{2})\$/","\$1 \$2 \$3 \$4 \$5",\$phone);

?>

but if the number is shorter then 10 digits will not work as you want.

best regards

Phil Jackson
10-21-2009, 05:44 PM
<?php

function phoneThing(\$str)
{
if(is_string(\$str))
{
\$strNew = "";
for(\$x=0; \$x!=strlen(\$str); \$x++)
{
if(\$x==(strlen(\$str)-4) || \$x==(strlen(\$str)-7) || \$x==(strlen(\$str)-10))
{
\$strNew .= \$str[\$x]." ";
}
else
{
\$strNew .= \$str[\$x];
}
}
return \$strNew;
}
else
{
}
}

echo phoneThing("0001112233445566");

//0001112 233 445 566
?>

Phil Jackson
10-21-2009, 05:47 PM
oops thats wrong read it wrong thought there was always 3 coupled digits

Phil Jackson
10-21-2009, 10:51 PM
getting there but a bit stuck...

<?php

function phoneThing(\$str)
{
if(is_string(\$str))
{
\$strNew = array();
\$strNewCount = 0;
\$start=1;
\$z=0;
for(\$x=0; \$strNewCount<=strlen(\$str); \$x++)
{
if((\$x & 1)==1) { \$z++; }
\$strNew[] = substr(\$str, - round(\$start-1), \$z+2);
\$strNewCount = (\$strNewCount+(\$z+2));
\$start=\$start+2+\$z;
}
return array_reverse(\$strNew);
}
else
{
}
}

echo implode(" ", phoneThing("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));

?>

returns

ABCDEF FGHIJ KLMNO OPQR STUV VWX YZ AB

Phil Jackson
10-21-2009, 11:01 PM
<?php

function phoneThing(\$str)
{
if(is_string(\$str))
{
\$strNew = array();
\$strNewCount = 0;
\$start=1;
\$z=0;
for(\$x=0; \$strNewCount<=strlen(\$str); \$x++)
{
if((\$x & 1)==1) { \$z++; }
\$strNew[] = substr(\$str, round(\$start-1), \$z+2);
\$strNewCount = (\$strNewCount+(\$z+2));
\$start=\$start+2+\$z;
}
return \$strNew;
}
else
{
}
}

echo implode(" ", phoneThing("abcdefghijklmnopqrstuvwxyz"));

?>

returns:

ab cde fgh ijkl mnop qrstu vwxyz

Phil Jackson
10-21-2009, 11:07 PM
EDIT: THIS ONE works exactly like how you wanted::

<?php
//www.actwebdesigns.co.uk
//luke@actwebdesigns.co.uk
//result given as array

function spacedResult(\$str)
{
if(is_string(\$str))
{
\$str = strrev(\$str);
\$strNew = array();
\$strNewCount = 0;
\$start=1;
\$z=0;
for(\$x=0; \$strNewCount<=strlen(\$str); \$x++)
{
if((\$x & 1)==0) { \$z++; }
\$strNew[] = substr(\$str, round(\$start-1), \$z+1);
\$strNewCount = (\$strNewCount+(\$z+1));
\$start=\$start+1+\$z;
}
\$strArray = array_reverse(\$strNew);
\$newArray = array();
foreach(\$strArray as \$obj)
{
\$newArray[] = strrev(\$obj);

}
return \$newArray;
}
else
{
}
}

echo implode("-", spacedResult("01623460397"));

//RETURNS 0-162-346-03-97

?>

Phil Jackson
10-21-2009, 11:10 PM
See above fixed it now

Arnaud
10-22-2009, 09:24 AM
Thank you guys! Phil, I will try your last example. That should be ok!

Arnaud
10-22-2009, 09:44 AM
Re-thinking about it made me come to another solution, which actually seems quicker and easier. I used strrev to reverse the number, then preg_split to separate the number in single pieces, then a simple foreach combined with an IF statement to add the spaces. Works fine like that and it actually also works for smaller numbers.

Thanks for pushing me to the right direction!

<?
\$number = "1234561231231212";
\$number = strrev(\$number);

\$chars = preg_split('//', \$number, -1, PREG_SPLIT_NO_EMPTY);

\$x=0;
foreach (\$chars as \$numbers) {
\$display .= \$numbers;
if ((\$x == 1) or (\$x == 3) or (\$x == 6) or (\$x == 9)) {
\$display .= " ";
}
\$x++;
}

\$display = strrev(\$display);
echo \$display;
//Displays: 123456 123 123 12 12
?>

:thumbsup:

Phil Jackson
10-22-2009, 06:26 PM
good to here it. The i was under the impression that you would not know the string length. With my above post, the input could be any length and the patten would still continue. But, glad you sorted it, it kept me amused for an hour :-)