PHP字符串解析 [英] PHP string parsing
本文介绍了PHP字符串解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用其唯一标识符来解析操作系统实例的列表.我正在寻找一种解析文本字符串并将值传递给两个变量的解决方案.要解析的字符串如下:
I am trying to parse a list of operating system instances with their unique identifiers. I am looking for a solution to parse a text string, and pass the values into two variables. The string to be parsed is as followed:
"Ubuntu 9.10" {40f2324d-a6b2-44e4-90c3-0c5fa82c987d}
推荐答案
I've been looking for an excuse to read the docs for sscanf():
sscanf($s, '"%[^"]" {%[^}]}', $os, $ident);
echo $os, "<br>", $ident;
后续行动:为了方便起见,在当前针对此问题的三个答案中:
Followup: For interest's sake, out of the three answers currently on this question:
sscanf: 0.92999792098999 seconds
preg_match: 4.73761510849 seconds
str_replace x2 + preg_split: 3.7644839286804 seconds
在此处进行基准测试.有趣的是,两个str_replace()
和一个preg_split()
比preg_match()
快.
Benchmark here. Funny that two str_replace()
and a preg_split()
are faster than the preg_match()
.
这篇关于PHP字符串解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文