[英]PHP preg_split() with conditional formatting
如果我必须解析包含可能包含分隔符语法的注释的字符串,我将如何在PHP中处理它?
让输入字符串为:
"56.82[This is a comment], 43.78, 62.55[Comment 2, but with delimiter identifiers], 33.21"
我怎么能解析这个字符串,以便我能像这样回应这两个部分:
56.82 - This is a comment
43.78
62.55 - Comment 2, but with delimiter identifiers
33.21
我认为使用preg_match_all
可能更容易。
preg_match_all("/[0-9.]+(\[.*?\])?/",$input,$matches);
通过这种方式,您可以遍历$matches
数组( var_dump
以查看结构),这比尝试拆分文本更可靠。
在使用preg_函数之前,可以使用preg_quote()来扩展字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.