Jump to content

Clean Mobile Number


jaymc

Recommended Posts

This may be a stupid question but whats the best way to clean a mobile number from invalid chars without messing up the mobile itself

 

Mobile numbers I am having to deal with are for example like this

 

[44] 07783784123

[44] 07783-784-123

[44] 07 783 784 123

[07783784123]

447783784123

 

The end result must be 07783784123

 

Some mobiles can have 10/11 chars in, so it may be difficult to strip out all the bad characters and then just split leaving the last 11 chars..

Link to comment
Share on other sites

It may be a quicker process if you just build an array and use str_replace;

 

<?php
function sleanMobileNumber($number) {
$replace = array('[44]', '[', ']', ' ', '-');
$mob_num = str_replace($replace, "", $number);
if(substr($mob_num, 0, 2) == '44') {
	$mob_num = substr_replace($mob_num, '', 0, 2)
}
}

Link to comment
Share on other sites

Or you could use:

 

$str = '344frf4335vev4554g256';
$str = preg_replace('/[^0-9]/', '', $str);

if (strlen($str) != 10 && strlen($str) != 11)
{
    print 'Invalid phone number...';
}

 

 

Link to comment
Share on other sites

Ah yes, perhaps this then:

 

$str = '344frf4335vev4554g256';

$str = preg_replace('/[^0-9]/', '', $str);
$str2 = str_replace('44', '', $str);

if (strlen($str2) != 10 && strlen($str2) != 11)
{
    print 'Invalid phone number...';
}

Link to comment
Share on other sites

Hah, this then:

 

$str = '344frf4335vev4554g256';

$str = preg_replace('/[^0-9]/', '', $str);
$str2 = preg_replace('/$44/', '', $str);

if (strlen($str2) != 10 && strlen($str2) != 11)
{
    print 'Invalid phone number...';
}

Link to comment
Share on other sites

If I understand correctly, would something like this be along the lines of what you are looking for?

 

Example:

$phoneNumber = array('[44] 07783784123', '[44] 07783-784-123', '[44] 07 783 784 123', '[07783784123]', '447783784123');
foreach($phoneNumber as $val){
$val = preg_replace('#[^0-9]#', '', $val);
$valLen = strlen($val);
$val = ($valLen == 10 || $valLen == 12 ? substr($val, -10) : ($valLen == 13 || $valLen == 11 ? substr($val, -11) : 'Illegal Format!'));
echo $val . "<br />\n";
}

 

Output:

07783784123
07783784123
07783784123
07783784123
7783784123

 

That ternary operator could also be replaced with the more traditionally readable if() {...}else{...} statement instead:

if($valLen == 10 || $valLen == 12){
	$val = substr($val, -10);
} else if($valLen == 13 || $valLen == 11){
	$val = substr($val, -11);
} else {
	$val = 'Incorrect Format!';
}

 

Just a note:

The end result must be 07783784123

 

I question how this would be with the last example being 447783784123 (unless I'm missing something here).

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.