There is a line like:

Номер клиента: 1234567890<br>Вид заявки: 

The number can take the form not only as written above, but also there are options

 +12345678901 +1(234) 567-89-00 1-234-567-89-00 1 234 567 89 00 

How to create a regular expression so that only the number is drawn, and the rest is not taken into account. Ideally, you need to bring the number to uniformity, but until it comes out. Pruning will not work because the beginning of the number may be different (the ending by the sign <you can cut off but I can’t think of anything further yet).

  • Sorry a little not completely the question put at the beginning of the line can be any text in Cyrillic and different characters are not just certain words. I tried to put the expression of any letters from ah instead of words. And it is strange that on the preg_replace locale it is running normally, but the server doesn’t want to cut it anyway, the text "client number" still remains in the database. - ASYOU

3 answers 3

You should separate the number pullout and its formatting. Of course, everything can be done in one regular schedule, but this solution will not look clean.

You can pull it out like this: /Номер клиента: \+?([0-9-() ]+)/ , part \+?([0-9-() ]+) which coincides with the phone number, which may contain a plus sign at the beginning (or it may not contain), and the phone number itself may consist of digits, spaces, hyphens and brackets. Like that:

 $input_string = 'Номер клиента: +1(234) 567-89-00<br>Вид заявки:'; $phone_number = preg_replace('/Номер клиента: \+?([0-9-() ]+)/', '$1', $input_string); 

Now this number must be formatted. Usually phone numbers lead to a numeric value, so it's best to simply remove the extra characters from the number using the str_replace() function:

 $clean_number = str_replace([' ', '-', '(', ')'], '', $phone_number); echo $clean_number; // 12345678900 

If you put a phone number in the database, choose some text field, otherwise those numbers that start from zero, as @Miron correctly noted, may later be incorrectly processed. Try to work with the phone number as a string, not as a number.

  • You can delete its formatting from the phone number only if you will never need to divide the phone number by the country / city / operator code, etc. under any circumstances, for example, the country code can be determined from the numbers 12345678900 impossible, since the codes of countries, cities, mobile operators, the phone number itself may consist of different numbers of characters. - Miron
  • @Miron, the author of the question said that the numbers are in an arbitrary format, so that the formatting may be completely absent. And this question, in principle, is solved by the substr() function, which, of course, is more convenient (more precisely, more rational) to use on the filtered number. - neluzhin
  • one
    Well, but then, if the number starts from zero, the variant you proposed with (int) str_replace () will cut leading zeros, which is not good either. Store phone numbers that are recorded in any format - it is impossible, due to the possibility of losing the leading zero. - Miron
  • @Miron, ah, and you are right. I did not even think that there are numbers starting with zero. Corrected the answer. - neluzhin

To bring the number to a single image, you should use libraries for formatting. For example, the popular library libphonenumber :

 $swissNumberStr = "0446681800"; $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); try { $swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH"); // Будет отображено "044 668 18 00" echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::NATIONAL); } catch (\libphonenumber\NumberParseException $e) { var_dump($e); } 
  • Did you test this library well? Something I doubt that she will be able to make out all possible options for entering the number by the user. - Miron
  • Naturally, this is not a golden bullet, but it is good at formatting numbers. I used the libphonenumber port for Python, which is also made according to the principles of the google library, and it coped normally. Of course, before sending the number to the library, you need to carry out at least some sort of data normalization in order to get more or less acceptable results at the output. - Egor Smolyakov
  • one
    In my opinion, it is simply impossible to “normalize” telephone numbers from different countries, since there is no single standard for recording them. It is better to oblige, force or unobtrusively ask the user to enter a number from 3 parts right away: country code, operator / city code, number itself. - Miron

I propose to the author of the question, nevertheless, to begin with, bring the base of phone numbers to a single form, so as not to store incorrect numbers in the database. If phone numbers can be from different countries, it is necessary to keep the country code separately from the number and operator / city code. Otherwise, problems may arise.

If the number is recorded in the international format, for example +12345678901 or +1 (234) 567-89-01, then you can contact the subscriber. But what to do if the number is written like this: (234) 567-89-01 or in general 567-89-01? It will be very difficult to get through to a subscriber with such a number, if not to say it is impossible. To keep such numbers is pointless, so it’s better to delete them immediately.

Never store phone numbers as a number. This is due to the fact that if the phone number starts from zero (s) when casting it to the int type, you will forever lose leading zeros. Phone numbers should be stored exclusively as a string.

And one more important point - standards for numbering telephone numbers of subscribers in different countries may differ significantly, and therefore, the numbers are best stored as 3 different parts: country code, city / operator code, telephone number.