View Full Version : Removing Space In Mobile Numbers
tomyknoker
08-21-2007, 03:25 AM
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?
$MobileNumber = $_SESSION['strMobileNumber'];
$trimmedMobile = ereg_replace( ' +', '', $MobileNumber );
james438
08-21-2007, 05:17 AM
I use
$MobileNumber=preg_replace('/[\s]{2,}/',' ',$MobileNumber);
the following also works and is more precise:
$MobileNumber=preg_replace('/[\040]{2,}/',' ',$MobileNumber);
or if you want to replace some octal code with octal code try this:
$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
$MobileNumber = preg_replace( '/\s\s+/', ' ', $MobileNumber); There are probably more, but I can't think of any more.
tomyknoker
08-21-2007, 06:00 AM
Hey James can you explain what they do? So do they all do this... 0400 000 000 to 400000000...
james438
08-21-2007, 06:13 AM
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.
tomyknoker
08-21-2007, 06:31 AM
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?
james438
08-21-2007, 07:44 AM
I'm in and out tonight with tests this week, but to answer your question
replace
$trimmedMobile = ereg_replace( ' +', '', $MobileNumber );with
$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).
preg_replace('/[^\d+]/', '', $num);... will remove any characters except digits or the + sign.
tomyknoker
08-21-2007, 03:21 PM
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...
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...
preg_replace(array('/[^\d+]/', '/^0+/'), '', $num);
tomyknoker
08-23-2007, 07:31 AM
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?
function pad_num($n, $l, $c = '0') {
$r = $n . '';
while(strlen($r) < $l)
$r = $c . $r;
return $r;
}Pad it to the specified length, e.g.:
pad_num($tel, 10);Do be aware that different countries may have different telephone number lengths though.
tomyknoker
08-23-2007, 08:44 AM
Actually yes have found that just recently as people from New Zealand can now sign up and they seem to be adding longer numbers, so will this kill their numbers?
Not that technique in general, but setting it to one specific length will. You'd need to somehow work out what length the telephone number should be. The approach of just making sure it starts with a zero is flawed, because it might be necessary to have more than one leading zero or none at all, again depending on location.
tomyknoker
08-23-2007, 09:36 AM
Oh ok so then what does the above technique actually do?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.