Preg_split正则表达式如何分割字符串? [英] Preg_split regular expression how to split a string?

查看:96
本文介绍了Preg_split正则表达式如何分割字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这样一行

#40:dfgdfhfg fgh dfg hfdgh fghdfg hfdg:dfghdfgh/fghdfgh fdghdfg fghdfghd ghdfghf(仅作为示例)

#40: dfgdfhfg fgh dfg hfdgh fghdfg hfdg : dfghdfgh / fghdfgh fdghdfg fghdfghd ghdfghf (just as an example)

如何用这样的正则表达式将其分开-

how can you separate it with a regular expression like this -

[0] => 40
[1] => dfgdfhfg fgh dfg hfdgh fghdfg hfdg : dfghdfgh / fghdfgh fdghdfg fghdfghd ghdfghf (just as an example)

使用preg_split 预先感谢

using preg_split thanks in advance

推荐答案

我不确定是否可以使用preg_split完成所需的操作,但可以使用preg_match进行以下操作:

I'm not sure if what you want can be done using preg_split but you can do it with preg_match like this:

$s = "#40: dfgdfhfg fgh dfg hfdgh fghdfg hfdg : dfghdfgh / fghdfgh fdghdfg fghdfghd ghdfghf (just as an example)"    
preg_match('/^\#([0-9]+)\:\s(.*)$/', $s, $matches);

您需要的两个部分将在$matches[1]$matches[2]

And the two parts you are after will be in $matches[1] and $matches[2]

这篇关于Preg_split正则表达式如何分割字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆