<div class="final-pro" itemprop="pro">
<meta itemprop="curr" content="yen">
<span style="font-family: yen">d </span>15,675
<span class="base-pro linethrough">
<span style="font-family: yen">d </span>14,999
</span>
</div>
我需要使用 preg_match_all 从上述 html 代码中剪辑值 15,675 和 14,999。我尝试了尽可能多的方法但失败了。欢迎伸出援手。
到目前为止我已经尝试过:
preg_match_all('/yen">d </span>(.*?)<\span/s',$con,$val);
最佳答案
$txt = '<div class="final-pro" itemprop="pro">
<meta itemprop="curr" content="yen">
<span style="font-family: yen">d </span>15,675
<span class="base-pro linethrough">
<span style="font-family: yen">d </span>14,999
</span>
</div>';
$matches = array();
preg_match_all('/[0-9,]+/', $txt, $matches);
print_r($matches);
它只是查找数字的 [0-9,]+
和 ,
而已。
输出
Array ( [0] =>
Array (
[0] => 15,675
[1] => 14,999
)
)
如果您需要更复杂的正则表达式来满足您的需求,您可以使用
preg_match_all('/font-family: yen">d <\/span>([0-9,]+)/', $txt, $matches);
编辑:
如果你想在整个 div 中找到这些数字,那么正则表达式需要更复杂
preg_match('/<div class\="final\-pro" itemprop="pro">.*?<\/span>([0-9,]+).*?<\/span>([0-9,]+).*?<\/div>/s', $txt, $matches);
看一下启用“单行模式”的 /s
修饰符。在此模式下,点与换行符匹配。
关于php - preg_match_all 函数来剪辑整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17587817/