Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts

    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
    Chuck Norris counted to infinity.
    Twice.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?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
        {
            return 
    "please enter string format";    
        }
    }

    echo 
    phoneThing("0001112233445566");

    //0001112 233 445 566
    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #5
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    oops thats wrong read it wrong thought there was always 3 coupled digits
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    getting there but a bit stuck...

    PHP Code:
    <?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
        {
            return 
    "please enter string format";    
        }
    }

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

    ?>
    returns

    Code:
    ABCDEF FGHIJ KLMNO OPQR STUV VWX YZ AB
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?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($strround($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
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #8
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    EDIT: THIS ONE works exactly like how you wanted::

    PHP Code:
    <?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($strround($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

    ?>
    Last edited by Phil Jackson; 10-21-2009 at 10:17 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    See above fixed it now
    Last edited by Phil Jackson; 10-21-2009 at 10:15 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #10
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Thank you guys! Phil, I will try your last example. That should be ok!
    Chuck Norris counted to infinity.
    Twice.

  • #11
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    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, -1PREG_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 
    ?>
    Chuck Norris counted to infinity.
    Twice.

  • #12
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    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 :-)
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 


  •  

    Posting Permissions

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