使用preg_match_all从html获取文本

时间:2013-02-15 16:51:54

标签: php regex preg-match-all

我有htmlcode如何使用PHP将700 mAh作为数组[0],BL-5CA作为数组[2],诺基亚作为数组[3]。好像这样

preg_match_all('~<span>capacity(.*?)mAh~si', $result, $matches[0]);
preg_match_all('~<span>Model(.*?)</span></div>~si', $result, $matches[1]);
preg_match_all('~<span>Brand(.*?)</span></div>~si', $result, $matches[2]);
它显示了我的空数组。请帮我解决一下

<div class="b-goods-specifications mod_cutted"> <div class="b-goods-specifications-item"> <ul class="b-goods-specifications-list"> <li class = "b-goods-specifications-row g-clearfix "> <div class="b-goods-specifications-cell"> <span> **capacity** </ span> </ div> <div class="b-goods-specifications-cell"> **700 mAh** </ div> </ li> <li class="b-goods-specifications-row g-clearfix"> <div class="b-goods-specifications-cell"> <span> **Model** </ span> </ div > <div class="b-goods-specifications-cell"> **BL-5CA** </ div> </ li> <li class="b-goods-specifications-row g-clearfix"> <div class = "b-goods-specifications-cell "> <span> **Brand** </ span> </ div> <div class="b-goods-specifications-cell"> **Nokia** </ div> </ li> <li class =" b-goods -specifications-row g-clearfix "> <div class="b-goods-specifications-cell"> <span> Type </ span> </ div> <div class="b-goods-specifications-cell"> Li -ion ​​</ div> </ li> </ ul> </ div> </ div>

1 个答案:

答案 0 :(得分:1)

使用PHP DOM,它比正则表达式更简单。

http://php.net/manual/en/book.dom.php