[英]Find and extract Matched values from String in PHP
我有如下文字
$str = '<div>
<div id="priceRangeWrapper">
<div id="priceSlider" min="0" max="0"></div>
</div>
</div>';
1)首先,我想從上面的字符串中獲取<div id="priceSlider" min="0" max="0"></div>
的位置,其中min和max值是隨機的。 Php的strpos()函數之類的東西,它以int的形式返回位置,例如
$pos = strpos($str, '<div id="priceSlider" min="0" max="0"></div>');
//but min and max values are random. I don't know what can be they
2)我想從上面的文本中獲取最小值和最大值 。 如何在PHP中使用/不使用正則表達式來獲得這兩個值?
不要使用正則表達式來解析HTML。 相反,這是DOMDocument的示例。
$doc = new DOMDocument();
$doc->loadHTML($str); // Load the HTML string from your post
$xpath = new DOMXPath($doc);
$node = $xpath->query('//div[@id="priceSlider"]')->item(0); // Get the <div>
// Print out the min and max attribute values
echo $node->getAttribute('min') . " " . $node->getAttribute('max');
您可以看到它在這里工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.