php 关联数组、正则表达式、数组
我目前有以下代码:
$content = "
<name>Manufacturer</name><value>John Deere</value><name>Year</name><value>2001</value><name>Location</name><value>NSW</value><name>Hours</name><value>6320</value>";
我需要找到一种方法来创建和数组为 name=>value
。例如制造商=>约翰迪尔
。
任何人都可以帮助我剪断一个简单的代码,我尝试了一些正则表达式,但甚至无法提取名称或值,例如:
$pattern = "/<name>Manufacturer<\/name><value>(.*)<\/value>/";
preg_match_all($pattern, $content, $matches);
$st_selval = $matches[1][0];
I currently have the following code :
$content = "
<name>Manufacturer</name><value>John Deere</value><name>Year</name><value>2001</value><name>Location</name><value>NSW</value><name>Hours</name><value>6320</value>";
I need to find a method to create and array as name=>value
. E.g Manufacturer => John Deere
.
Can anyone help me with a simple code snipped I tried some regex but doesn't even work to extract the names or values, e.g.:
$pattern = "/<name>Manufacturer<\/name><value>(.*)<\/value>/";
preg_match_all($pattern, $content, $matches);
$st_selval = $matches[1][0];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您不想为此使用正则表达式。尝试类似 SimpleXML
编辑
好吧,为什么不从这个开始:
编辑 2
尽管使用正则表达式发布的一些答案可能有效,但您应该养成使用正确工具来完成工作的习惯,并且正则表达式不是解析 XML 的正确工具。
You don't want to use regex for this. Try out something like SimpleXML
EDIT
Well, why don't you start with this:
EDIT 2
Despite the fact that some of the answers posted using regular expression MAY work, you should get in the habit of using the correct tool for the job and regular expressions are not the correct tool for parsing of XML.
我正在使用您的
$content
变量:I'm using your
$content
variable:这个问题比较简单,可以通过正则表达式来解决。应该是:
问候
rbo
This is rather simple, can be solved by regex. Should be:
Regards
rbo
首先,永远不要使用 正则表达式 来解析 xml ...
您可以使用 XPATH 查询来完成此操作...
首先,将内容包装在根标记中以使解析器满意(如果还没有):
然后,加载文档
然后,初始化 XPATH
编写您的查询:
然后执行它:
如果我正确执行了 xpath,则
$manufacturer
应该是John Deere
...您可以在
$manufacturer
上查看文档a href="http://php.net/manual/en/class.domxpath.php" rel="nofollow noreferrer">DomXpath,a XPath 基础入门,以及 一堆 XPath 示例...编辑:这行不通(PHP 不支持该语法(以下同级)。您可以这样做,而不是xpath查询:
First of all, never use regex to parse xml...
You could do this with an XPATH query...
First, wrap the content in a root tag to make the parser happy (if it doesn't already have it):
Then, load the document
Then, initialize the XPATH
Write your query:
Then, execute it:
If I did the xpath right, it
$manufacturer
should beJohn Deere
...You can see the docs on DomXpath, a basic primer on XPath, and a bunch of XPath examples...
Edit: That won't work (PHP doesn't support that syntax (following-sibling). You could do this instead of the xpath query:
我认为这就是您正在寻找的:
?>
如果数组有很多元素,请不要在 for 循环中使用 count() 函数,请先计算值,然后将其用作常量。
I think this is what you're looking for:
?>
If your array has a lot of elements dont use the count() function in the for loop, calculate the value first and then use it as a constant.
我将进行编辑,因为我的 PHP 是错误的,但这里有一些 PHP(伪)代码可以提供一些指导。
$arr
是要存储名称/值对的数组。I'll edit as my PHP is wrong, but here's some PHP (pseudo-)code to give some direction.
$arr
is the array you want to store the name/value pairs.使用
XMLReader
:输出为:
Using
XMLReader
:The output is: