0

I have the following PHP code that is suppose to remove the 'excess' text when a user enters it in a field. I have the following code as follows:

$mobileNumber = '00 356 99048123';
$excessMobileNumbers = array(//remove excess items in mobile number such as dialing codes and empty spaces
   // ' ' => '',
    '00356' => '',
    '+356' => '',
    '356' => '',  
    '00' => '',
);

The output is 99048123

The above code works well as it the number 99048123 doesn't contain 356 or 00.

But when I use this number 00 356 99008123, the number 99008123 contains 00. I want it only to remove the 00 in the 00 i.e. starting from the left hand side and leaving without removing the 00 in the 99008123.

How do I go about it? I use the array as a 'filtering' system.

Thanks

Clarification It is not only for 00 even for 356, if the number is 99048123 it works fine. If this number 99035612 since it has 356 withing it it does not work.

SOLUTION I discovered this solution which seems to work for my problem.

$mobileNumber = '00 356 99048000';
$mobileNumber = str_replace(' ','',$mobileNumber); // UPDATE
$excessMobileNumbers = substr($mobileNumber, 0, -8);
$mobileNumber = str_replace($excessMobileNumbers,'',$mobileNumber);
echo $mobileNumber;

Thank you all for your contribution.

5
  • 2
    If your only interested in the last part why don't you explode the string on empty space an take the last element? Commented Aug 14, 2014 at 10:59
  • You'd be much better off doing this with a regular expression so that you can specify that the 00 needs to be at the beginning of the string. Commented Aug 14, 2014 at 11:01
  • 1
    Remove all non numeric characters and then grab last 8 of string Commented Aug 14, 2014 at 11:05
  • use regex and preg_replace - see below Commented Aug 14, 2014 at 11:10
  • Explode should work in the example given, but the OP indicated this is a user-entered field. They could enter 0035699048123 or 00-356-99048123. @Anthony has the right idea. It would not require any maintenance for adding prefixes to filter. Commented Aug 14, 2014 at 11:14

5 Answers 5

2

You could use regex to strip out all non-numeric characters:

 preg_replace("/[^0-9]/", "", $mobileNumber);

and then use substr to grab the part that you want:

 $my_number = substr($mobileNumber, -8);

This way if a number is passed in like:

 $mobileNumber = '00 356 99-048-123';

or any other non-numeric characters are inside the part you actually want, after stripping out those characters you always know the last 8 characters are the numeric digits you are after.

Sign up to request clarification or add additional context in comments.

4 Comments

but he also want to remove the 00 and the 00356?! right? So that doesn't work for the OP
Right, which won't be in the last 8 characters, hence the substr.
moblie numbers can differ in length - they are not always 8 digits
So its better to guess all possible prefixes and hope for the best? And mobile numbers aren't random in length, their length is based on dialing plan, which OP can assume. If they were just any length, how would your phone know when you're done dialing?
0

As some suggested, try this:

$mobileNumber = '00 356 99048123';
$mobileNumberParts = explode(" ", $mobileNumber);
echo end($mobileNumberParts);

output: 99048123

Comments

0

As @ende-neu suggests simply explode out the string on a space and just take the last element in the array.

For the example you give this would look like:

  $mobileNumber = '00 356 99048123';
  $mobile_array = explode(' ',$mobile_number);
  $my_number = end($mobile_array); //will give you 99048123
  echo $my_number;

Although to be honest you're probably better off with a regular expression type approach or use a solid library such as libphonenumber for PHP

Comments

0

Use preg_replace to remove all of your listed "prefixes" (only when at the beginning (^))

$number = preg_replace('/^(00|00365|\+365|365)/', '', $mobileNumber);

That is much safer than just splitting at white space because some people might add more white space in between the last part to improve readability.

Comments

0

I discovered this solution and it seems to work for my case

$mobileNumber = '00 356 99048000';
$mobileNumber = preg_replace("/[^0-9]/", '', $mobileNumber);
$excessMobileCharactors = substr($mobileNumber, 0, -8);
$mobileNumber = str_replace($excessMobileCharactors,'',$mobileNumber);
echo $mobileNumber;

Thank you all for your kind help.

4 Comments

@Anthony answer is better as what if user enter '00 356 99 04 8000' Of course if your carrier number can be more/less than 8 digits then none of the answers is good.
I updated my solution to include $mobileNumber = str_replace(' ','',$mobileNumber); to remove the extra spaces. In the case of my country all numbers mobile numbers are 8 charactors long.
Still Anthony's answer is better as it removes all non digit characters. ex +356/00-04-800
Thank you. I improved it again on Anthony's idea above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.