php - How to get first numeric character with preg_match? -


I have a phone number. If the first numeric character of a number is not 1, then I have to add it. I do not know how to do it with a preg_match, or is there any other way?

  $ first_number = $ number; If (preg_match ('some', $ before_number, $ result)) {$ first_number = '1' $ result [1]; }    

There is no need to go for regex here. You can do this:

  if ($ first_number [0]! = '1') {$ first_number = '1' $ First_number; }   

But if the number can be the top position or sign to + :

  if (preg_match ( '/ ^ (\ D *) (\ d) /', $ first_number, $ match)) $ first_number = $ match [1]. ' 1 '. $ Match [2]; }    

Comments