preg_match a string with repeated data in it
For example i have next string :
2556974513|0123254852|1025635214|1236521025|0124576832
in the string must be 10
digits then |
and this formula may be repeated n
times, how to match it?
maybe there is an easiest way to do that, than this :
preg_match('/开发者_StackOverflow社区^[\d]{10}|[\d]{10}| ... [\d]{10}$/', $string);
Thanks.
You will want something like this:
^(?:\d{10}\|)+\d{10}$
The \d{10}
at the end is there since there is no |
character at the end. The +
will match one or more of the preceding pattern. If it is valid to match only one group of 10 digits then replace the +
with *
which will match zero or more times.
Note that the |
character needs to be escaped, because it is used as an OR operator in regex.
This means "one or more of (10 digits then pipe)":
(\d{10}\|)+
Edit: It's not clear from your question if the tailing pipe will be there or not. If not, then:
(\d{10}\|)*(\d{10})
Also, if you need to extract the values anyway, you may as well just do:
foreach (explode('|', $string) as $value) {
if (preg_match('/^\d{10}$/', $value)) {
...
}
}
精彩评论