SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: Help (regexps?)

  1. #1
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help (regexps?)

    Hi,

    ok, basically i have a number in the format

    07736 284 827

    and i want to turn it into

    +447736284827

    any ideas how? i'm stuck on removing the 0 if there is one as sometimes the number will be entered as 7736... etc

    Alex
    Nearly 7 years old!

  2. #2
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a bit confused..


    00 to 44? What for?


    What exactly are you trying to do? Get rid of zero's? Zero's and spaces? Zero's at the begining and spaces?


    Regards,
    Someonewhois

    P.S. Long time no see.

  3. #3
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, i guess i didn't explain properly

    in the UK, the format of a mobile (cell) number is usually used/wrote as

    07747 372 482 for example

    a 0, always followed by 4 digits (network code) and then 6 digits (number)

    i want to convert this to a international format number, i.e

    +447747372482

    exaxtly the same, but no preceding 0 and a +44 on the end

    the problem is removing the 0 only if it is there

    i.e

    <?php

    if (0 is there) {
    remove 0
    }
    $mnumber = "+44" . $mnumber;
    ?>

    if that makes sense
    Nearly 7 years old!

  4. #4
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmm, my regex isn't that good, but this should do it:


    PHP Code:
    $txt "07747 372 482"// I use quotes on numbers as if it's a string, not an int most of the time..

    $txt ereg_replcae("^0"""$txt);

    $txt "+44".$txt
    Will that work?

    "^0" meaning if the 0 is at the begining?

    Regards,
    Someonewhois

    P.S. What did you have that didn't work?


    [Edit: Oh, I added 1 "4" instead of 2 accidentally ]

    [Edit2: Argh, I want ereg_replcae, not preg_replace.. ]

  5. #5
    ********* Celica Lover Coomer's Avatar
    Join Date
    Apr 2002
    Location
    Not worth the drive
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do this:

    PHP Code:
    <?php
    $txt 
    str_replace(" """$txt);
    if(
    substr($txt01) == "0"){
       
    $txt substr($txt1);
    }
    $txt "+44" $txt;
    ?>
    + Celica =
    6G Celicas :: My '94-99 Toyota Celica resource

  6. #6
    ********* Celica Lover Coomer's Avatar
    Join Date
    Apr 2002
    Location
    Not worth the drive
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like someonewhois beat me to it. You forgot to remove the spaces in the $txt variable though.
    + Celica =
    6G Celicas :: My '94-99 Toyota Celica resource

  7. #7
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, and if you REALLY want that if() to check if it has it or not:

    PHP Code:
    $txt "07747 372 482"

    if (
    ereg("^0"$txt))
    {
    $txt ereg_replcae("^0"""$txt); 
    }
    $txt str_replace(" """$txt);
    $txt "+44".$txt
    BTW, that won't give spaces now

  8. #8
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Coomer
    Looks like someonewhois beat me to it.
    I just had to help him

    AlexC was the one who got me started both at PHP, and when I was getting so many errors, he's the one who sent me to SPF, and I'm glad he did!

  9. #9
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe... been a while since i've done much coding (and needed help..) - ok, someonewhois - your solution produces this error:

    Code:
    07732495603
    Warning: No ending delimiter '^' found in /home/teravox/public_html/fonewow/channels/mobileprocess.php on line 44
    +44
    Any ideas? - i'm going to try coomers idea now

    Thanks :-D
    Nearly 7 years old!

  10. #10
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    coomer, you are a genius - with a little work from both of your solutions i got the script working, the number outputs perfectly now

    Thankyou both of you! :-D
    Nearly 7 years old!

  11. #11
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't use regex much


    PHP Manual:


    ereg ("^abc", $string);
    /* Returns true if "abc";
    is found at the beginning of $string. */


    Beats me


    Glad you figured it out


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
  •