SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    splitting a value from the database

    I have telephone numbers stored in a database.
    it looks like this i.e. '715144336'
    The first two numbers are the area-code ('71') the last seven in the normal number.

    However on the edit.php page I want to split this number into a textfield for area-code and a textfield for normal number.
    How can I split this value into say $tel_area ('71') and $tel_norm ('5144336')?

    Any help will be appreciated!

  2. #2
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use the ever-so-handy (tm) left() function in your query

    SELECT LEFT(715144336,2) FROM tablename (etcetcetc)

    that will return '71'

    and for the rest of it...

    SELECT RIGHT(715144336,7) FROM tablename (etcetcetc)

    that will return '5144336'

    thats how to do it easily in mysql
    Aaron "Theiggsta" Kalin
    Pixel Martini
    Ruby and Rails Developer

  3. #3
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You could try:

    $tel = ereg("([0-9]{3})(.)", $tel, $tel_n)
    $tel_area = $tel_n[1];
    $tel_norm = $tel_n[2];


    I'm just learning regular expressions really, but it may work. What it does it search the $tel for 3 digits, then another set of any character s of any length. then the first brackets may it the 1 value of the $tel_n array and then the second is the second part.

    To print the number out you could either:

    print $tel_n[0];

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unless I am missing something here it would be easiest to use substr() for this, if it is always the case that the frst two numbers are the areacode.
    PHP Code:
    $str_phone '715551212';
    $area_code substr($str_phone02);
    $phone_num substr($str_phone2strlen($str_phone)); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That'd work freddy...


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
  •