SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inserting character at certain position in string

    I'm looking for some help on a small problem.
    I have a string that might look like $str = 08000830 (It's always 8 numbers in a row)
    What I would like to do is insert a dot after the two first numbers, a dash after the fourth, and another dot after the sixth, so the string would end up looking like:

    08.00-08.30

    How can this be accomplished?

  2. #2
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 

    function convert_number($num)
    {
    $new_num substr($num0,2) . '.';
    $new_num .= substr($num2,2) . '-';
    $new_num .= substr($num4,2) . '.';
    $new_num .= substr($num6,2);

    return 
    $new_num;
    }

    echo 
    convert_number('08000830');

     
    ?>
    I think there are better ways to do it , this one was the first to pop into my head.

    hope this helps

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had tried messing with substr(), but couldn't get it to function properly.
    Now I understand how it works. Thanks for the help


Bookmarks

Posting Permissions

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