我们假设我们有一个nodeValue] 92/100'或者' some / all'或者' 92/100'并且'一些/所有' (字母/数字周围的那些空格)。
例如:
<?php
$strhtml='
<div>
<div>
<p>92/100</p>
</div>
<div>
<p>some/all</p>
</div>
<div>
<p> 92 / 100</p>
</div>
<div>
<p>some /all </p>
</div>
</div>';
$doc = new DOMDocument();
@$doc->loadHtml($strhtml);
$doc->preserveWhiteSpace = false;
$xpath = new DOMXPath( $doc );
$nodelist = $xpath->query('//div/div/p[1]');
foreach( $nodelist as $node ) {
$result = $node->nodeValue;
}
echo $result;
如何仅选择&#39; 92&#39;和/或&#39;某些&#39;?
感谢。
答案 0 :(得分:0)
这或多或少是你要求的吗?重新调整/
?
$strhtml='
<div>
<div>
<p>92/100</p>
</div>
<div>
<p>some/all</p>
</div>
<div>
<p> 92 / 100</p>
</div>
<div>
<p>some /all </p>
</div>
</div>';
$doc = new DOMDocument();
$doc->loadHtml($strhtml);
$doc->preserveWhiteSpace = false;
$xpath = new DOMXPath( $doc );
$nodelist = $xpath->query('//div/div/p[1]');
foreach( $nodelist as $node ) {
list( $keep, $junk )=explode('/',$node->nodeValue );
$result = trim( $keep );
}
echo $result;