Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Removing Space In Mobile Numbers

  1. #1
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Removing Space In Mobile Numbers

    I have the following script which works for removing extra spaces in mobile numbers... Just wondering if anyone know sof other ways to do it?

    PHP Code:
    $MobileNumber $_SESSION['strMobileNumber'];
            
    $trimmedMobile ereg_replace' +'''$MobileNumber ); 

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,287
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default

    I use

    PHP Code:
    $MobileNumber=preg_replace('/[\s]{2,}/',' ',$MobileNumber); 
    the following also works and is more precise:
    PHP Code:
    $MobileNumber=preg_replace('/[\040]{2,}/',' ',$MobileNumber); 
    or if you want to replace some octal code with octal code try this:
    PHP Code:
    $sp="\040";
    $MobileNumber=preg_replace('/[\040]{2,}/','$sp',$MobileNumber); 
    They all do the same thing though.

    ...mostly.

    one more that I found on php.net
    PHP Code:
    $MobileNumber preg_replace'/\s\s+/'' '$MobileNumber); 
    There are probably more, but I can't think of any more.
    Last edited by james438; 08-21-2007 at 05:42 AM.

  3. #3
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey James can you explain what they do? So do they all do this... 0400 000 000 to 400000000...

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,287
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default

    They all do the same thing that your script does. More specifically:

    Example 1 looks for areas in $MobileNumbers that has sets of two or more whitespace ( space, formfeed, newline, carriage return, horizontal tab, and vertical tab) usually they are just spaces though.

    Example 2 looks specifically for sets of two or more spaces. \040 is the octal code for space.

    Example 3 is just to show a trick where you can replace thhe replacement with octal code

    Example 4 is just something I found on php.net (the site of champions ) it looks for patterns of whitespace that is followed immediately by a set of whitespace. I think any of the above examples do the same thing better though.

    All 4 examples will trim parts of the variable where there are sets of 2+ spaces and replace the set with a single space.

  5. #5
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ahhhhhh ok great! What mine does is remove all white space, how would I change yours to do that? And I know it's probably a stupid question but which would be more correct to use? ereg_replace or preg_replace?

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,287
    Thanks
    97
    Thanked 104 Times in 102 Posts

    Default

    I'm in and out tonight with tests this week, but to answer your question
    replace
    PHP Code:
    $trimmedMobile ereg_replace' +'''$MobileNumber ); 
    with
    PHP Code:
    $trimmedMobile ereg_replace' +'' '$MobileNumber ); 
    I misread your code earlier. I thought that is what you were doing anyway.

    preg_replace() is usually faster, but they both operate a little differently. I am more familiar with PCRE expressions, but only a little. Twey is the obvious master. Preg_replace() uses Perl Compatible Regular Expressions (PCRE).

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    preg_replace('/[^\d+]/', '', $num);
    ... will remove any characters except digits or the + sign.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey Twey... Yea that works nicely... I'm kind of not getting the whole preg_replace thing, have read the manual, let me just think out loud... So with preg_replace you have the pattern to follow, replacement and the target yes? So in the above you are, set the string with / / and then you put the pattern inside the [ ], and from there you use the carat symbol to say from the beginning of the string... But not sure what it asks... From the beginning of the string look for Digits and +?? Or does the \ infront of the the d+ protect those characters? Also I am trying to work out how to remove the leading 0 in the mobile numbers but can't seem to do it...

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    From the beginning of the string look for Digits and +??
    Replace all characters that aren't one of ([^...]): digits (\d) or the plus symbol (+) with ''.
    Also I am trying to work out how to remove the leading 0 in the mobile numbers but can't seem to do it...
    Code:
    preg_replace(array('/[^\d+]/', '/^0+/'), '', $num);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey Twey I know I originally wanted the leading zero removed but now am having issues with other things, so how can I make sure that even if a user doesn't put in a leading 0 say they just enter 415397555 that it will always add a leading 0 so will be 0415975555?

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
  •