2010-08-18 107 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循環,首先計算該值,然後將其用作常量。