Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: String manipulation on a (phone) number

1. ## String manipulation on a (phone) number

Hi Folks,

Quick question.

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

PHP Code:
``` <? \$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

• 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

• PHP Code:
``` <?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

• PHP Code:
``` <?phpfunction 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    {        return "please enter string format";        }}echo phoneThing("0001112233445566");//0001112 233 445 566?> ```

• oops thats wrong read it wrong thought there was always 3 coupled digits

• getting there but a bit stuck...

PHP Code:
``` <?phpfunction 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    {        return "please enter string format";        }}echo implode(" ", phoneThing("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));?> ```
returns

Code:
`ABCDEF FGHIJ KLMNO OPQR STUV VWX YZ AB`

• PHP Code:
``` <?phpfunction 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    {        return "please enter string format";        }}echo implode(" ", phoneThing("abcdefghijklmnopqrstuvwxyz"));?> ```
returns:

Code:
`ab cde fgh ijkl mnop qrstu vwxyz`

• EDIT: THIS ONE works exactly like how you wanted::

PHP Code:
``` <?php//www.actwebdesigns.co.uk//luke@actwebdesigns.co.uk//result given as arrayfunction 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    {        return "please enter string format";        }}echo implode("-", spacedResult("01623460397"));//RETURNS 0-162-346-03-97?> ```

• See above fixed it now

• Thank you guys! Phil, I will try your last example. That should be ok!

• 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!

PHP Code:
``` <? \$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  ?> ```

• 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 :-)

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•