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
234 views
in Technique[技术] by (71.8m points)

php - Split a string using preg_split

Im looking for a way to split the following string after each set of numbers using regex. I am fairly new to this and Im having a hard time understanding how to use the correct regex format.

$string = '521158525 Interest Being Subordinated: 521855248 Benefiting Interest: 511589923';

preg_split("/([0-9])/", $string, 0, PREG_SPLIT_NO_EMPTY);
question from:https://stackoverflow.com/questions/65888008/split-a-string-using-preg-split

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

1 Reply

0 votes
by (71.8m points)

To split after each set of numbers, you might use a pattern to match only digits between word boundaries.

Then use K to forget what is matched until so far follwed by matching optional horizontal whitespace chars to not get trailing whitespaces after the split.

$string = '521158525 Interest Being Subordinated: 521855248 Benefiting Interest: 511589923';
$result = preg_split(
    "d+h*K",
    $string,
    0,
    PREG_SPLIT_NO_EMPTY
);

print_r($result);

Output

Array
(
    [0] => 521158525
    [1] => Interest Being Subordinated: 521855248
    [2] => Benefiting Interest: 511589923
)

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

...