【发布时间】:2015-09-17 09:05:02
【问题描述】:
array { [0]=>
City
Street
Zip
我有这个,但我需要的是这样的:
array { [0]=>
'city'=>City
'Street'=>Street
'zip'=>Zip
我正在使用 PHPDOM 来获取我添加到数组中的 html
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $element) {
if (strpos($element->getAttribute('class'), 'contentSection') !== false) {
$string = $element->C14N();
}
}
$array = explode("</p>", $string);
print_r($array);
我正在使用 DOM 处理的 HTML 部分
<div class="contentSection">
City
<br>
Street
<br>
ZIP
<p class="Separator">
</p>
City
<br>
Street
<br>
ZIP
我尝试做的事情是
$dom = new DOMDocument('1.0');
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $element) {
if (strpos($element->getAttribute('class'), 'contentSection') !== false) {
$string = $element->C14N();
}
}
$keys = array("city","street", "zip");
$array = array_explode_with_keys("<br>", $keys, $string);
print_r($array);
function array_explode_with_keys($delimiter, $keys, $string){
$return = array();
$pieces = explode($delimiter,$string);
foreach($pieces as $i=>$piece){
if($i<count($keys)) {
$return[$keys[$i]] = $piece;
} else {
$return[$i] = $piece;
}
}
return $return;
}
?>
但这会返回:
Array ( [city] =>
city[street] =>
street [zip] =>
zip
city [3] =>
street [4] =>
zip
所以第二部分出了问题
【问题讨论】:
-
你能给我们看看html部分吗?