解析格式化字符串——隔离带引号的子字符串和大括号 UUID
我正在尝试解析操作系统实例及其唯一标识符的列表。我正在寻找一种解决方案来解析文本字符串,并将值传递给两个变量。待解析的字符串如下:
"Ubuntu 9.10" {40f2324d-a6b2-44e4-90c3-0c5fa82c987d}
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}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
稚然2024-09-10 11:07:55
这使用 str_replace 和 preg_split:
$s = "\"Ubuntu 9.10\" {40f2324d-a6b2-44e4-90c3-0c5fa82c987d}";
$s = str_replace("\" ", "|", $s); // substitute middle quotation mark and space for a delimiter
$s = str_replace("\"", "", $s); // remove quotation marks
$vars = preg_split('/\|/', $s); // split by delimiter
print_r($vars);
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我一直在寻找借口阅读 sscanf() 的文档:
后续: 出于兴趣,目前对此问题有三个答案:
此处为基准。有趣的是,两个
str_replace()
和一个preg_split()
比preg_match()
更快。I've been looking for an excuse to read the docs for sscanf():
Followup: For interest's sake, out of the three answers currently on this question:
Benchmark here. Funny that two
str_replace()
and apreg_split()
are faster than thepreg_match()
.