how to add space inside this string ?

Discussion in 'PHP' started by ramysarwat, Sep 28, 2010.

  1. #1
    how to add space between "1234" and "ab" in this example ?

    1234ab
     
    ramysarwat, Sep 28, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Will it always be in that format 4 numbers followed by 2 letters?
     
    danx10, Sep 29, 2010 IP
  3. ramysarwat

    ramysarwat Peon

    Messages:
    164
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    it will be some numbers contain "," but the end is always 2 letters

    so its something like this 1234,56ab
     
    ramysarwat, Sep 29, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    $str = '1234,56ab';
    
    $ex = explode(',', $str);
    
    $numbers = $ex[0];
    
    $letters = $ex[1];
    PHP:
     
    danx10, Sep 29, 2010 IP
  5. maneetpuri

    maneetpuri Active Member

    Messages:
    152
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Hi,

    You can add another string in between that will only be the space or pad a space on right of the first string or left for the second string.

    Cheers,

    ~Maneet
     
    maneetpuri, Sep 29, 2010 IP
  6. sunlcik

    sunlcik Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    <?php
    /*
    [nannvmen.com] (C)2010-2012 nannvmen.com Inc.
    This is a freeware
    $str - your string
    $num - the end letters num 
    $separation - what you want to add
    $Id:test.php 2010-09-29 16:04:07Z nannvmen.com $
    */
    
    function lick_split( $str,$num, $separation )
    {
            if( empty( $separation ) || empty( $str ) )
            {
                    echo 'function lick_split parameter error';
                    exit;
            }
    
            if( $num > 0 )
            {
                    echo 'num should be a negative number!';
                    exit;
            }
            $prefix = substr( $str,0,$num );
            $suffix = substr( $str,$num );
            return $prefix.$separation.$suffix;
    }
    /* eg 1.
    $newstr = lick_split('my name is sunlicksunlcik',-7,'! NOT ');
    echo $newstr;
    exit;
    */
    /* eg 2.
    $newstr = lick_split('1234ab',-2,',56');
    echo $newstr;
    exit;
    */
    
    PHP:
    This is only a simple way,there are many ways.
    If you have any questions ,feel free to contact me.

    Regards,
    Wu
     
    sunlcik, Sep 29, 2010 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    If there will always be 2 letters on the end, you can use substr.

    <?php
    
    $str = '1234,56ab';
    
    $new_str = substr($str, 0, strlen($str) - 2) . ' ' . substr($str, -2);
    
    ?>
    Code (markup):
     
    joebert, Sep 29, 2010 IP
  8. TDub

    TDub Peon

    Messages:
    30
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Regular expression matching will work. This code adds a space when the input has two alpha characters (case-insensitive) appearing last.

    
    <?php
    $strInput = '1234,ab';
    preg_match('#(.*)([A-Z]{2,2})#i', $strInput, $strInputArray);
    $strOutput = $strInputArray[1] . ' ' . $strInputArray[2];
    
    Code (markup):
     
    TDub, Oct 3, 2010 IP