我需要使用 php 从 span 标签中获取多个值,例如:
<div>
<span class='name'>name value</span>
<span class='city'>city name</span>
<span class='street'>street name</span>
<span class='phone'>+17240000000</span>
</div>
<div>
<span class='name'>name value 2</span>
<span class='city'>city name 2</span>
<span class='street'>street name 2</span>
<span class='phone'>+17240000000 2</span>
</div>
我必须使用跨度类获取跨度值,我尝试使用此代码来获取该值,并且它仅适用于 1 个值,例如“名称”:
<?
$html = "<div>
<span class='name'>name value</span>
<span class='city'>city name</span>
<span class='street'>street name</span>
<span class='phone'>+17240000000</span>
</div>
<div>
<span class='name'>name value 2</span>
<span class='city'>city name 2</span>
<span class='street'>street name 2</span>
<span class='phone'>+17240000000 2</span>
</div>";
$dom = new DOMDocument();
@$dom->loadHTML($html);
$dom_xpath = new DOMXPath($dom);
$entries = $dom_xpath->evaluate("//span[@class='name']");
foreach ($entries as $entry) {
echo $entry->nodeValue."<br />";
}
?>
有没有人可以帮助我修改代码,以便我可以获得所有值
谢谢