2010-08-18 59 views
0

我现在有以下代码: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。例如,Manufacturer => John Deere

谁能帮我用一个简单的代码剪断我尝试了一些正则表达式,但甚至不工作,提取你不想使用正则表达式这个名称或值,例如:

$pattern = "/<name>Manufacturer<\/name><value>(.*)<\/value>/"; 
preg_match_all($pattern, $content, $matches); 
$st_selval = $matches[1][0]; 

回答

3

我用你的$content变量:

$preg1 = preg_match_all('#<name>([^<]+)#', $content, $name_arr); 
$preg2 = preg_match_all('#<value>([^<]+)#', $content, $val_arr); 
$array = array_combine($name_arr[1], $val_arr[1]); 
+0

快速简单:)非常感谢! – Michael 2010-08-18 17:28:00

5

。尝试像SimpleXML

编辑
好了,你为什么不开始与此:

<?php 

$content = "<root>" . $content . "</root>"; 
$xml = new SimpleXMLElement($c); 
print_r($xml); 

?> 

EDIT 2
尽管一些答案使用正则表达式发布可以工作,你应该习惯使用正确的工具来完成这项工作,而正则表达式不是解析XML的正确工具。

+0

好的,但我如何将结果关联到单个数组? – Michael 2010-08-18 17:11:20

+0

也内容似乎不是有效的XML所以我得到一些像SimpleXMLElement :: __构造()[simplexmlelement .--构造]的错误:实体:行3:分析器错误:额外的内容在第13行的C:\ wamp \ www \ index.php中的文档 – Michael 2010-08-18 17:16:14

+0

这看起来不像“真正的世界xml”,它只是一系列标签。 – 2010-08-18 17:32:03

0

我将编辑为我的PHP是错误的,但这里有一些PHP(伪)代码给一些方向。

$pattern = '|<name>([^<]*)</name>\s*<value>([^<]*)</value>|' 
preg_match_all($pattern, $content, $matches, PREG_SET_ORDER); 
for($i = 0; $i < count($matches); $i++) { 
    $arr[$matches[$i][1]] = $matches[$i][2]; 
} 

$arr是要存储名称/值对的数组。

1

首先,从来没有使用regex to parse xml ...

您可以用XPath查询做到这一点...

首先,包装在一个根标签的内容,使解析器快乐(如果它还没有的话):

$content = '<root>' . $content . '</root>'; 

然后,加载文档

$dom = new DomDocument(); 
$dom->loadXml($content); 

然后,初始化XPATH

$xpath = new DomXpath($dom); 

写您的查询:

$xpathQuery = '//name[text()="Manufacturer"]/follwing-sibling::value/text()'; 

然后执行它:

$manufacturer = $xpath->evaluate($xpathQuery); 

如果我做了XPath的权利,这$manufacturer应该John Deere .. 。

你可以看到DomXpath的文档,一个basic primer on XPatha bunch of XPath examples ...

编辑:那是不行的(PHP不支持语法(以下同胞)。你可以这样做,而不是xpath查询:

$xpathQuery = '//name[text()="Manufacturer"]'; 
$elements = $xpath->query($xpathQuery); 
$manufacturer = $elements->item(0)->nextSibling->nodeValue; 
+0

这篇文章实际上说,对简单的字符串使用正则表达式是可以的。 – palswim 2010-08-18 17:22:28

+0

不幸的是它不工作它说 警告:domdocument :: domdocument()期望至少有一个参数,0在第7行给出的C:\ wamp \ www \ index.php中 致命错误:调用未定义的方法domdocument ::第8行的C:\ wamp \ www \ index.php中的loadXml() – Michael 2010-08-18 17:24:40

+1

您使用的是哪个版本的PHP? 4?不,恕我直言,即使是简单的xml字符串,也不会使用正则表达式。这就好像说可以使用goto来做简单的事情...... – ircmaxell 2010-08-18 17:32:07

2

这很简单,可以通过正则表达式来解决。应该是:

$name = '<name>\s*([^<]+)</name>\s*'; 
$value = '<value>\s*([^<]+)</value>\s*'; 

$pattern = "|$name $value|"; 
preg_match_all($pattern, $content, $matches); 

# create hash 
$stuff = array_combine($matches[1], $matches[2]); 

# display 
var_dump($stuff); 

问候

RBO

0

使用XMLReader

$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>'; 
$content = '<content>' . $content . '</content>'; 
$output = array(); 

$reader = new XMLReader(); 
$reader->XML($content); 
$currentKey = null; 
$currentValue = null; 
while ($reader->read()) { 
    switch ($reader->name) { 
     case 'name': 
      $reader->read(); 
      $currentKey = $reader->value; 
      $reader->read(); 
      break; 
     case 'value': 
      $reader->read(); 
      $currentValue = $reader->value; 
      $reader->read(); 
      break; 
    } 
    if (isset($currentKey) && isset($currentValue)) { 
     $output[$currentKey] = $currentValue; 
     $currentKey = null; 
     $currentValue = null; 
    } 
} 

print_r($output); 

输出是:

Array 
(
    [Manufacturer] => John Deere 
    [Year] => 2001 
    [Location] => NSW 
    [Hours] => 6320 
) 
1

我认为这是你在找什么:

<?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>"; 
$pattern = "(\<name\>(\w*)\<\/name\>\<value\>(\w*)\<\/value\>)"; 
preg_match_all($pattern, $content, $matches); 

$arr = array(); 

for ($i=0; $i<count($matches); $i++){ 
    $arr[$matches[1][$i]] = $matches[2][$i];  
} 

/* This is an example on how to use it */ 
echo "Location: " . $arr["Location"] . "<br><br>"; 

/* This is the array */ 
print_r($arr); 

>

如果阵列有很多的元素不使用count()函数中? for循环,首先计算该值,然后将其用作常量。