Welcome Guest, Not a member yet? Register   Sign In
Removing spaces from phone numbers
#1

[eluser]Rurik[/eluser]
Ok, so this is not strictly related to code igniter, but php forumns are usually rather unhelpful.

I have a phone number, lets say 0401 000 000.

I need to remove the spaces from these and put the number back together (its a string and i convert to int later).

I have tried using explode, and I have tried using substr but whenever I put it back together, it becomes 401000, losing the leading 0 and the second lot of trailing 0's.

For example, I am currently using the following code:

Code:
$string = "1";
$num1 = substr($d['contactno'], 0, strpos($d['contactno'], " ", 0));
$num2 = substr($d['contactno'], strpos($d['contactno'], " ", 0)+1,strrpos($d['contactno'], " ")-strpos($d['contactno'], " ", 0));
$num3 = substr($d['contactno'], strrpos($d['contactno'], " ")+1);
if($num1 != $num3)
{
$string = $string.$num1.$num2.$num3;
}
echo $d['contactno']."</br>";
echo $num1."</br>";
echo $num2."</br>";
echo $num3."</br>";
$string = substr($string, 1);
$d['contactno'] = (int)$string;
echo $d['contactno']."</br>";

and the results are as follows:

0401 000 100
0401
000
100
401000

Any ideas for how I could go about this? Its really starting to frustrate me.

Regards,

Rurik
#2

[eluser]CroNiX[/eluser]
Code:
$num = '0123 456 789';
  
//remove spaces
$num = str_replace(' ', '', $num);

//get first 4 digits
$n1 = substr($num, 0, 4);
//get next 3
$n2 = substr($num, 4, 3);
//get last 3
$n3 = substr($num, 7, 3);

echo "$n1$n2$n3"; //0123456789
#3

[eluser]Rurik[/eluser]
Thanks CroNiX, this looks like it will work perfectly for me.

hmm, using str_replace now makes it show the trailing three numbers, but it still cuts off the leading 0. Would this be because if me casting the string as an int?

Any ideas of getting around that, seeing as how I need it as an int in the database?

Rurik
#4

[eluser]Rurik[/eluser]
Problem solved, turns out sql could handle me passing in a string as long as it was all numbers.
#5

[eluser]g1smd[/eluser]
Make sure you store the number in full E.164 format with country code so that you can then apply area code changes to your stored data as they happen.




Theme © iAndrew 2016 - Forum software by © MyBB