我試圖讓在這個標籤所需的內容:如何使用preg match獲取此html標記的內容?
<p class="address">
desired content
</p>
這是我的嘗試:
preg_match_all("/\<p class=\"address\">(.*)\<\/p\>/", $contents, $matches);
但是$匹配的數組爲空。請幫忙。
感謝
我試圖讓在這個標籤所需的內容:如何使用preg match獲取此html標記的內容?
<p class="address">
desired content
</p>
這是我的嘗試:
preg_match_all("/\<p class=\"address\">(.*)\<\/p\>/", $contents, $matches);
但是$匹配的數組爲空。請幫忙。
感謝
你可以試試這個:
$contents = '<p class="address">desired content</p>';
$res = preg_match_all("/\<p class=\"address\">(.*)\<\/p\>/s", $contents, $matches);
var_dump($res);
var_dump($matches);
$匹配是不是空的,對不對?
仍然爲空...... – 2012-03-02 09:47:42
你錯過了換行符。 – rodneyrehm 2012-03-02 09:48:59
試試這樣:$ res = preg_match_all(「/ \
(。*)\ <\/p\>/s」,$ contents,$ matches); – Oleg 2012-03-02 09:55:18
.
也匹配\n
- 見modifiers.*?
(ungreedy運營商),以不妨礙捕獲組匹配</p>
像這樣:
<?php
$contents = '<p class="address">
desired content
</p>';
$res = preg_match_all('#<p class="address">(.*?)</p>#s', $contents, $matches);
var_dump($matches);
[xpath的](http://schlitt.info/opensource/blog/0704_xpath.html)[替代:(https://gist.github.com/ 1358174)'xpath_match_all('// p [@ class =「address」]',$ html);' – Gordon 2012-03-02 10:17:22