简体   繁体   中英

How can I use preg_match_all to seperate this string in PHP?

I'm wondering how you can use preg_match_all to seperate this string

2:18 textextextextextext,sdfdsfd:,fdg

So it will return an array that looks like this:

array(
       0 => 2
       1 => 18
       2 => textextextextextext,sdfdsfd:,fdg
)

Basically removing the first colon

您可以使用格式化的字符串:

print_r(sscanf("2:18 textextextextextext,sdfdsfd:,fdg", "%d:%d %s"));

First of all, what you want to use is preg_match() and not preg_match_all() (based on your desired output).

You could then use a regex like:

(\d+):(\d+)\s*(.*)

Live Demo

Which in PHP using preg_match() would look like this:

$pattern = "/(\d+):(\d+)\s*(.*)/";
$string = "2:18 textextextextextext,sdfdsfd:,fdg";
preg_match($pattern, $string, $matches);

Doing print_r($matches) would output:

Array
(
    [0] => 2:18 textextextextextext,sdfdsfd:,fdg
    [1] => 2
    [2] => 18
    [3] => textextextextextext,sdfdsfd:,fdg
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM