Home php PHP regular expression, split the string according to certain symbols with one...

PHP regular expression, split the string according to certain symbols with one request, strokes on the point of the point

Author

Date

Category

There are phone numbers in format (string):

0930001122, 0960001122

0930001122; 0960001122

0930001122.0960001122.

Accordingly, I want to split into an array of symbols: ,. ;
On the comma and point with the comma, it enters the array, but on the symbol of the point – does not want.
Infa pulls up from the database table (row type)

$ kitchen = preg_split ("/, /", $ obj- & gt; phone); // Sing the comma
If (Count ($ Kitchen) == 1) {
  $ kitchen = preg_split ("/; /", $ obj- & gt; phone); // If the length of the array is equal to one, it means there or one phone, or the previous split did not work, trying to split on a point with a comma.
}
If (Count ($ Kitchen) == 1) {
  $ kitchen = preg_split ("/./", $ Obj- & gt; phone); // If the array length is one, it means there or one phone, or previous splits did not work. We are trying to divide the string on the point.
}

With such code, the first two formats of phones are divided into an array, and the third is simply written as 0, emptiness.


Answer 1, Authority 100%

It is not clear what is your problem. The code is quite worker, but can be reduced to:

$ phone = '0930001122, 0960001122,0930001122; 0960001122; 0930001122.0960001122. ';
$ kitchen = preg_split ("/ (\ s +)? [.;,] (\ s +)? /", $ Phone);
Print_R ($ Kitchen);

We get the result

array
(
  [0] = & gt; 0930001122.
  [1] = & gt; 0960001122.
  [2] = & gt; 0930001122.
  [3] = & gt; 0960001122.
  [4] = & gt; 0930001122.
  [5] = & gt; 0960001122.
  [6] = & gt;
)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions