Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

php - preg_match to match substring of three numbers consecutively?

I have a string $text_arr="101104105106109111112113114116117120122123124" fairly big string

If i want to split three numbers from them like 101,104,105 and store them in $array .What should i do?

I tried doing this:

preg_match_all('/[0-9]{3}$/',"$text_arr",$array); 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The easiest way to do this is with preg_split()Docs:

$result = preg_split('/(d{3})/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

See it working, or the result:

Array
(
    [0] => 101
    [1] => 104
    [2] => 105
    [3] => 106
    [4] => 109
    [5] => 111
    [6] => 112
    [7] => 113
    [8] => 114
    [9] => 116
    [10] => 117
    [11] => 120
    [12] => 122
    [13] => 123
    [14] => 124
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...