您好我需要删除屏幕废料网站的链接,这里是输出源。
<div class="FourDayForecastContainerInner">
<span class="day">Friday</span>
<a href="forecastPublicExtended.asp#Period4" target="_blank">
<img src="./images/wimages/b_rain.gif" class="thumbnail">
</a>
<span class="hi">
<span style="width:24px;">Hi</span>
19 / 66
</span>
<span class="lo">
<span style="width:24px;">Lo</span>
16 / 60
</span>
<span class="description">
Sunny Breaks, showers
</span>
</div>
<div class="FourDayForecastContainerInner">
<span class="day">Saturday</span>
这是我的代码我使用phpquery
$doc = phpQuery::newDocumentHTML( $e );
$containers = pq('.FourDayForecastContainerInner', $doc);
foreach( $containers as $container ) {
$div = pq('span', $container);
$img = pq('img', $container);
$div->eq(0)
->removeAttr('style')
->addClass('day')
->html(
pq( 'u', $div->eq(0) )
->html()
);
$img->eq(0)
->removeAttr('style')
->removeAttr('height')
->removeAttr('width')
->removeAttr('alt')
->addClass('thumbnail')
->html( pq( 'img', $img->eq(0)) );
$div->eq(1)
->removeAttr('style')
->addClass('hi');
$div->eq(3)
->removeAttr('style')
->addClass('lo');
$div->eq(5)
->removeAttr('style')
->addClass('description');
}
print $doc;
我已经设法删除所有属性样式高度宽度等但我似乎无法删除a href 非常感谢你的帮助
答案 0 :(得分:0)
$html = 'Your HTMl CODE HERE';
$exp = "~<a.*>~isU";
$html = preg_replace($exp,"", $html);
$exp = "~</a>~isU";
$html = preg_replace($exp,"", $html);
echo $html
这将完全删除链接
答案 1 :(得分:0)
我用你的示例代码尝试了它并且它有效。这是输出
<div class='FourDayForecastContainerInner'>
<span class='day'>Friday</span>
<img src='./images/wimages/b_rain.gif' class='thumbnail'>
<span class='hi'>
<span style='width:24px;'>Hi</span>
19 / 66
</span>
<span class='lo'>
<span style='width:24px;'>Lo</span>
16 / 60
</span>
<span class='description'>
Sunny Breaks, showers
</span>
</div>
<div class='FourDayForecastContainerInner'>
<span class='day'>Saturday</span><div class='FourDayForecastContainerInner'>
<span class='day'>Friday</span>
<img src='./images/wimages/b_rain.gif' class='thumbnail'>
<span class='hi'>
<span style='width:24px;'>Hi</span>
19 / 66
</span>
<span class='lo'>
<span style='width:24px;'>Lo</span>
16 / 60
</span>
<span class='description'>
Sunny Breaks, showers
</span>
</div>
<div class='FourDayForecastContainerInner'>
<span class='day'>Saturday</span>
你做的事情太漫长而且很无聊。使用正则表达式替换链接。
答案 2 :(得分:0)
以下代码是否符合您的要求? (在“for”循环结束时添加)
$imghtml = pq('a', $container)->html();
pq($container)->prepend($imghtml);
pq('a', $container)->remove();
注意:phpquery似乎不支持jquery detach()
答案 3 :(得分:0)
我遇到了同样的问题,我想分享我的解决方案。我的目标是从一些SoundCloud嵌入代码的标题部分删除所有标签。 HTML看起来像这样:
<object height="81" width="100%">
... a bunch of embed code ...
</object>
<span>
<a href="http://soundcloud.com/minimal/mike-ink-silver">Mike Ink _ Silver</a>
by
<a href="http://soundcloud.com/minimal">MINIMAL</a>
</span>
在上面的HTML结尾处,您可以看到标题不仅包含一个,而且还包含两个链接。我的目标是剥离它们。
假设HTML已分配给PHP变量$ text,这就是我的工作方式:
$doc = phpQuery::newDocument($text);
$soundcloud_title = strip_tags((string) $doc->find('span'));
print($soundcloud_title);
// outputs: Mike Ink _ Silver by MINIMAL
我知道这并没有直接回答这个问题。事实上,我正在使用strip_tags删除链接而不是使用phpquery,但我希望它可以帮助其他正在寻找相同答案的程序员。
快乐的编码!