I have a ul list like this:
<ul>
<li>
<div class="time">18:45</div>
<div class="info">description goes here</div>
<div class="clearAll"></div>
</li>
<li>
<div class="time">19:15</div>
<div class="info">some info</div>
<div class="clearAll"></div>
</li>
</ul>
How can I turn this into an array like this:
$array = array(
1 => array('18:45','description goes here');
1 => array('19:15','some info');
);
Stay away from a regex for this. DOMDocument
is your friend:
$dom = new DOMDocument;
$dom->loadHTML( $theHTMLstring );
$array = array();
foreach ( $dom->getElementsByTagName('li') as $li ) {
$divs = $li->getElementsByTagName('div');
$array[] = array(
$divs->item(0)->textContent,
$divs->item(1)->textContent
);
}
See it here in action: http://codepad.viper-7.com/5ExOqJ
By not using regex:
$sx = new SimpleXMLElement($xml);
foreach ($sx->xpath('//li') as $node) {
$time = current($node->xpath("div[@class='time']"));
$time = "$time";
$info = current($node->xpath("div[@class='info']"));
$info = "$info";
$data[] = array($time, $info);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.