我正在使用file_get_contents
来抓取一个HTML页面。我希望刮除仅在<pre>
和</pre>
标记之间。任何想法如何实现这一目标?代码如下:
$html = file_get_contents('http://www.atletiek.co.za/.....htm');
$tags = explode(' ', $html);
foreach ($tags as $tag) {
// skip scripts
if (strpos($tag, 'script') !== false) {
continue;
}
// get text
$text = strip_tags(' ' . $tag);
// only if text present remember
if (trim($text) != '') $texts[] = $text;
}
print_r($text);
答案 0 :(得分:0)
如果它足够你可以使用正则表达式。
$s = 'test <pre>this is simple</pre> test <pre class="tricky">this is' . "\n" . 'tricky</pre> test';
if (preg_match_all('#<pre(?: [^>]*)?>(.*?)</pre>#msi', $s, $m)) {
print_r($m[1]);
}
显示
Array
(
[0] => this is simple
[1] => this is
tricky
)
但请阅读此内容 - https://stackoverflow.com/a/1732454/437763
您可能需要XPath - http://php.net/manual/en/domxpath.query.php
答案 1 :(得分:0)
我通过添加我想要排除的所有标记和属性来解决它。我使用if (strpos($tag, 'script') !== false) {
来表示我不想加载的所有标签。它对我有用,因为只有大约5或6个人。