Convert a number to international, PHP - php

In PHP what is the quickest way to convert an mobile number to international format:
So 07123456789 becomes 447123456789.
I have tried a few ways and cant seem to get it to work.
This is current script:
if(strlen($gsm) > 2) {
if(!substr_compare($gsm, "07", 0, 2, false)) {
unset($gsm);
}
elseif (substr_compare($gsm, "07", 0, 3, true)) {
if(strlen($gsm) == 11) {
return "447" . substr($gsm, 2);
}
}
}
Note: This script only runs if the number matches a regex.

Something like this, perhaps?
$intl_number = preg_replace('/^0/','44',$uk_number);
or if you specifically only want to do UK mobile numbers:
$intl_number = preg_replace('/^07/','447',$uk_mob_number);
(note: I'm assuming UK-specific since you specified '44' in the question)
This does use Regex, but should be pretty quick in execution speed since it is anchored to the begining of the string.

Fastest way that comes to mind for me, as long as you are shre the number matches the format 07xxxxxx at this point:
$number = "07123456789";
$number = '44'.substr($number,1);

There is a PEAR package for validating international telephone numbers.

Related

Why isn't mod, is_int and ctype_digit working as expected in my scenario?

I have numbers coming through that I'm checking whether or not their divisible is a number without leaving over any decimal places. I.e. I check if the number is divisible by 9, 8, 7, 6.75, etc. I don't want to use regex.
Examples
mod (my preferred choice)
if (344.25 % 6.75 == 0) {
print_r("No decimals"); //Should print because the calculation is 51, a whole number?
}
is_int
if (is_int(344.25/6.75)) {
print_r("No decimals"); //Should also print?
}
ctype_digit
if (ctype_digit(344.25/6.75)) {
print_r("No decimals"); //Should also print?
}
I've also tried is_numeric with no luck either.
I've ended up using if(ctype_digit(strval(344.25/6.75))) {...} and it's working well.

How to get log() of a very big number (PHP)?

I've looked at php-big numbers, BC Math, and GMP for dealing with very big numbers in php. But none seem to have a function equivilent to php's log(). For example I want to do this:
$result = log($bigNumber, 2);
Would anyone know of an alternate way to get the log base 2 of a arbitray precision point number in php? Maybe Ive missed a function, or library, or formula.
edit: php-bignumbers seems to have a log base 10 function only log10()
In general if you want to implement your high precision log own calculation, I'd suggest 1st use the basic features of logarithm:
log_a(x) = log_b(x) / log_b(a) |=> thus you can recalulate logarith to any base
log(x*y) = log(x) + log(y)
log(a**n) = n*log(a)
where log_a(x) - meaning logarithm to the base a of x; log means natural logarithm
So log(1000000000000000000000.123) = 21*log(1.000000000000000000000123)
and for high precision of log(1+x)
use algorithm referenced at
http://en.wikipedia.org/wiki/Natural_logarithm#High_precision
One solution combining the suggestions so far would be to use this formula:
log2($num) = log10($num) / log10(2)
in conjunction with php-big numbers since it has a pre-made log10 function.
eg, after installing the php-big numbers library, use:
$log2 = log10($bigNum) / log10(2);
Personally I've decided to use different math/logic so as to not need the log function, and just using bcmath for the big numbers.
One of the great things about base 2 is that counting and shifting become part of the tool set.
So one way to get a 'log2' of a number is to convert it to a binary string and count the bits.
You can accomplish this equivalently by dividing by 2 in a loop. But it seems to me that counting would be more efficient.
gmp_scan0 and gmp_scan1 can be used if you are counting from the right. But you'd have to somehow convert the mixed bits to all ones and zeroes.
But using gmp_strval(num, 2), you can produce a string and do a strpos on it.
if the whole value is being converted, you can do a (strlen - 1) on it.
Obviously this only works when you want an integer log.
I've had a very similar problem just recently.. and so I just scaled the number considerably in order to use the inbuild log to find the fractional part.. (I prefere the log10 for some reason.. don't ask... people are strange, me too)
I hope this is selfexplanatory enough..
it returns a float value (since that's what I needed)
function gmp_log($num, $base=10, $full=true)
{
if($base == 10)
$string = gmp_strval($num);
else
$string = gmp_strval($num,$base);
$intpart = strlen($string)-1;
if(!$full)
return $intpart;
if($base ==10)
{
$string = substr_replace($string, ".", 1, 0);
$number = floatval($string);
$lg = $intpart + log10($number);
return $lg;
}
else
{
$string = gmp_strval($num);
$intpart = strlen($string)-1;
$string = substr_replace($string, ".", 1, 0);
$number = floatval($string);
$lg = $intpart + log10($number);
$lb = $lg / log10($base);
return $lb;
}
}
it's quick, it's dirty... but it works well enough to get the log of some RSA sized integers ;)
usage is straight forward as well
$N = gmp_init("11002930366353704069");
echo gmp_log($N,10)."\n";
echo gmp_log($N,10, false)."\n";
echo gmp_log($N,2)."\n";
echo gmp_log($N,16)."\n";
returns
19.041508364472
19
63.254521604973
15.813630401243

Phone number format received from twilio

I am storing sms received from twilio in a database so I can use them later. When I did this in the sandbox it worked. However when I upgraded to a regular phone number the number received is the same as was sent to, but +1 (or for xxxxxxxxxx where the x's are the original number, it looks more like 1xxxxxxxxxx+)
I therefore changed the mysql_query to the following: but it is still not working. What can be done to recognize that this is the original phone number?
<?php
$starttime = time();
$number = $_POST['number'];
$number1 = "1" . $number;
$number2 = $number . "1";
$number3 = "+1" . $number;
$number4 = $number . "+1";
$number5 = "+" . $number . "1";
$number6 = "1" . $number . "+";
$number7 = $number."1+";
$received = mysql_query("SELECT * FROM sms_received
WHERE (responder='$number' OR responder='$number1'
OR responder='$number2' OR responder='$number3'
OR responder='$number4' OR responder='$number5'
OR responder='$number6' OR responder='$number6')
AND (body='y' OR body='yes' OR body='Y' OR body='Yes' OR 'yea' OR 'Yea')
AND timestamp BETWEEN ".date('Y-m-d H:i:s', strtotime($starttime))." AND NOW()");
?>
But still, nothing is being received. Any ideas how else I can check whether an sms has been received from the user? I can see in the database that it's there... but the mysql isn't finding it. It worked before, when the number sent was identical to the number received from, but with the added +1 it screws it up. (the code before just had WHERE responder = '$number' and it worked, but the additional variables didn't help it).
Does this code have too many OR's? Is that even a problem?
UPDATE:
Thanks, here is the function I'm using to strip the number down to xxxxxxxxxx format, before saving it to the database:
function checkPhone($responder){
$items = Array('/\ /', '/\+/', '/\-/', '/\./', '/\,/', '/\(/', '/\)/', '/[a-zA-Z]/');
$clean = preg_replace($items, '', $responder);
if (substr($clean, 0, 1) == "1") {
return substr($clean, 1, 10);
}
else {
return substr($clean, 0, 10);
}
}
$number = checkPhone($responder);
Twilio returns numbers in a format called E.164, which is an internationally recognized standard for phone number formatting.
In general, it's best practice to standardize the number to E164 BEFORE you store it in the database. That way you don't have to worry about storing different data with two different copies of the same number - eg 925-555-1234 and (925) 5551234.
Google has a libphonenumber library that will convert numbers for you. It works with Javascript, C++, Java, and Python.
If you are using PHP, and only using US/Canadian numbers, you can write a function to normalize phone numbers, that does something like the following:
- Strip out all non number characters from the phone number
(parentheses, dashes, spaces) - you can use a function like preg_replace
- if the phone number begins with a +1, do nothing
- if the phone number begins with a 1, add a +
- else, add a +1 to the beginning of the number.
- finally, store it in the database.
I hope that helps - please let me know if you have more questions.
Kevin
Your last or is redundantly $number6, it should be $number7.
Aside from that, you can do a few different things, such as in:
responder in ('$number', '$number1', '$number2', '$number3', '$number4', '$number5', '$number6', '$number7')
Or something like this:
responder like '%$number%'
Use a regular expression.
preg_match_all("{[0-9]+}",$number,$m);
$norm_num="+".implode($m[0]);
if(strlen($norm_num)<6)
exit('Too short!');
mysql_query("SELECT * FROM sms_received
WHERE responder='%$norm_num%'
AND body IN ('y','yes','Y','Yes','yea','Yea')
AND timestamp BETWEEN ".date('Y-m-d H:i:s', strtotime($starttime))." AND NOW()");

How to convert an integer value to word format using php5?

I want to convert a number to word format.
For example:
$count = 5;
echo "Hello Mr user this is your ".$count." review."
I need output like this...
"Hello Mr user this is your fifth review."
Check this in another StackOverflow thread:
Convert a number to its string representation
There is no built in function in php to make that. But try external lib like : http://pear.php.net/package/Numbers_Words
If you're referring to a built-in function, no, PHP does not have one. But You can create your own.
If you're looking at only English numbers, it's not much of an issue. But if you need to deal with a foreign language (like Arabic), you have to do a bit of extra work since numbers and objects have genders. So the algorithm gets a little more complex.
Just for the record, I'm working on producing an open source conversion tool for Arabic.
I know this isn't exactly what you're after, but I picked this up from the comments on php.net somewhere (can't find source), it works for output like 1st, 243rd and 85th.
function ordinalize($num)
{
if ( ! is_numeric($num)) return $num;
if ($num % 100 >= 11 and $num % 100 <= 13)
{
return $num."th";
}
elseif ( $num % 10 == 1 )
{
return $num."st";
}
elseif ( $num % 10 == 2 )
{
return $num."nd";
}
elseif ( $num % 10 == 3 )
{
return $num."rd";
}
else
{
return $num."th";
}
}
You might want to even consider this format anyways for readability and simplicity, depending on how high you expect the numbers to be. If they are less than 100, you should be able to write your own function easily. However, if they can get really high:
"Hello Mr user this is your three thousand four hundred and twenty-fifth review."
Sounds a bit awkward :)
Hope this helps some.

Whats the cleanest way to convert a 5-7 digit number into xxx/xxx/xxx format in php?

I have sets of 5, 6 and 7 digit numbers. I need them to be displayed in the 000/000/000 format. So for example:
12345 would be displayed as 000/012/345
and
9876543 would be displayed as 009/876/543
I know how to do this in a messy way, involving a series of if/else statements, and strlen functions, but there has to be a cleaner way involving regex that Im not seeing.
sprintf and modulo is one option
function formatMyNumber($num)
{
return sprintf('%03d/%03d/%03d',
$num / 1000000,
($num / 1000) % 1000,
$num % 1000);
}
$padded = str_pad($number, 9, '0', STR_PAD_LEFT);
$split = str_split($padded, 3);
$formatted = implode('/', $split);
You asked for a regex solution, and I love playing with them, so here is a regex solution!
I show it for educational (and fun) purpose only, just use Adam's solution, clean, readable and fast.
function FormatWithSlashes($number)
{
return substr(preg_replace('/(\d{3})?(\d{3})?(\d{3})$/', '$1/$2/$3',
'0000' . $number),
-11, 11);
}
$numbers = Array(12345, 345678, 9876543);
foreach ($numbers as $val)
{
$r = FormatWithSlashes($val);
echo "<p>$r</p>";
}
OK, people are throwing stuff out, so I will too!
number_format would be great, because it accepts a thousands separator, but it doesn't do padding zeroes like sprintf and the like. So here's what I came up with for a one-liner:
function fmt($x) {
return substr(number_format($x+1000000000, 0, ".", "/"), 2);
}
Minor improvement to PhiLho's suggestion:
You can avoid the substr by changing the regex to:
function FormatWithSlashes($number)
{
return preg_replace('/^0*(\d{3})(\d{3})(\d{3})$/', '$1/$2/$3',
'0000' . $number);
}
I also removed the ? after each of the first two capture groups because, when given a 5, 6, or 7 digit number (as specified in the question), this will always have at least 9 digits to work with. If you want to guard against the possibility of receiving a smaller input number, run the regex against '000000000' . $number instead.
Alternately, you could use
substr('0000' . $number, -9, 9);
and then splice the slashes in at the appropriate places with substr_replace, which I suspect may be the fastest way to do this (no need to run regexes or do division), but that's really just getting into pointless optimization, as any of the solutions presented will still be much faster than establishing a network connection to the server.
This would be how I would write it if using Perl 5.10 .
use 5.010;
sub myformat(_;$){
# prepend with zeros
my $_ = 0 x ( 9-length($_[0]) ) . $_[0];
my $join = $_[1] // '/'; # using the 'defined or' operator `//`
# m// in a list context returns ($1,$2,$3,...)
join $join, m/ ^ (\d{3}) (\d{3}) (\d{3}) $ /x;
}
Tested with:
$_ = 11111;
say myformat;
say myformat(2222);
say myformat(33333,';');
say $_;
returns:
000/011/111
000/002/222
000;033;333
11111
Back-ported to Perl 5.8 :
sub myformat(;$$){
local $_ = #_ ? $_[0] : $_
# prepend with zeros
$_ = 0 x ( 9-length($_) ) . $_;
my $join = defined($_[1]) ? $_[1] :'/';
# m// in a list context returns ($1,$2,$3,...)
join $join, m/ ^ (\d{3}) (\d{3}) (\d{3}) $ /x;
}
Here's how I'd do it in python (sorry I don't know PHP as well). I'm sure you can convert it.
def convert(num): #num is an integer
a = str(num)
s = "0"*(9-len(a)) + a
return "%s/%s/%s" % (s[:3], s[3:6], s[6:9])
This just pads the number to have length 9, then splits the substrings.
That being said, it seems the modulo answer is a bit better.

Categories