【发布时间】:2013-09-15 18:54:33
【问题描述】:
我正在使用一个piratebay 反向代理脚本,该脚本使用curl 来加载thepiratebay。它还有一个删除/替换广告的选项,但它使用的是 str_replace,我想知道是否有更好的方法来做到这一点。
这是当前脚本删除不需要的内容的方式
<?php
function remove_bloat($toremove){
include("configurationfile.php");
//Fix /static links so they work in subdirs
$toremove = str_replace("src=\"/static","src=\"static" , $toremove);
$toremove = str_replace("href=\"/static","href=\"static" , $toremove);
$toremove = str_replace("url(\"/static","url(\"static" , $toremove);
$toremove = str_replace("url('/static","url('static" , $toremove);
$toremove = str_replace("//static.thepiratebay.se/","static/" , $toremove);
//Remove Ads
$toremove = str_replace('<iframe src="http://cdn1.adexprt.com/exo_na/center.html" width="728" height="90" frameborder="0" scrolling="no"></iframe>', $leaderboard, $toremove);
$toremove = str_replace('<iframe src="http://cdn2.adexprt.com/exo_na/center.html" width="728" height="90" frameborder="0" scrolling="no"></iframe>', $leaderboard, $toremove);
$toremove = str_replace('<iframe src="http://cdn1.adexprt.com/exo_na/sky2.html" width="160" height="600" frameborder="0" scrolling="no" style="padding-top: 100px"></iframe>', $rightside, $toremove);
$toremove = str_replace('<iframe src="http://cdn2.adexprt.com/exo_na/sky2.html" width="160" height="600" frameborder="0" scrolling="no" style="padding-top: 100px"></iframe>', $rightside, $toremove);
$toremove = str_replace('<iframe src="http://cdn1.adexprt.com/exo_na/sky1.html" width="120" height="600" frameborder="0" scrolling="no"></iframe>', $leftside, $toremove);
$toremove = str_replace('<iframe src="http://cdn2.adexprt.com/exo_na/sky1.html" width="120" height="600" frameborder="0" scrolling="no"></iframe>', $leftside, $toremove);
$toremove = str_replace('<iframe src="http://cdn1.adexprt.com/exo_na/bottom.html" width="728" height="90" frameborder="0" scrolling="no"></iframe>', $leaderboard, $toremove);
$toremove = str_replace('<iframe src="http://cdn2.adexprt.com/exo_na/bottom.html" width="728" height="90" frameborder="0" scrolling="no"></iframe>', $leaderboard, $toremove);
$toremove = str_replace('<iframe src="http://cdn1.adexprt.com/exo_na/top.html" width="468" height="60" frameborder="0" scrolling="no"></iframe>', $topsmall, $toremove);
$toremove = str_replace('<iframe src="http://cdn2.adexprt.com/exo_na/top.html" width="468" height="60" frameborder="0" scrolling="no"></iframe>', $topsmall, $toremove);
$toremove = str_replace('sessionHash', '', $toremove);
$toremove = str_replace('baypops.com', '', $toremove);
return $toremove;
}
str_replace 过去只是删除广告,但我创建了自己的变量并添加了它们,现在用我自己的内容替换广告。 ($leaderboard, $leftside, $rightside, $topsmall)
但是我发现了更多通过 curl 加载的广告并且也想替换它们,问题是这组广告没有静态 URL,并且在所有 iframe 源中将页面标题作为变量如下图...
<iframe src="http://cdn1.adexprt.com/ividi/ividi.php?b=top&n=This_Is_the_End_%282013%29_720p_BrRip_x264_-_YIFY" width="469" height="60" frameborder="0" scrolling="no"></iframe>
相同的广告位置不同的页面
<iframe src="http://cdn2.adexprt.com/ividi/ividi.php?b=top&n=Jobs_2013_HDRip_x264_AC3-JYK" width="469" height="60" frameborder="0" scrolling="no"></iframe>
同样的广告不同的页面
<iframe src="http://cdn2.adexprt.com/ividi/ividi.php?b=top&n=World_War_Z_%282013%29_UNRATED_1080p_BrRip_x264_-_YIFY" width="469" height="60" frameborder="0" scrolling="no"></iframe>
你可以看到唯一改变的是子 url cdn 和 src 的结尾部分。
所以我正在考虑使用 preg_replace 而不是 str_replace 并尝试使用仅用于 iframe src 的正则表达式并根据宽度和高度进行替换。
所以类似于以下内容
$toremove = preg_replace('<iframe src="/regular expression ?/" width="469" height="60" frameborder="0" scrolling="no"></iframe>', 'replaced content', $toremove);
这是否可行以及我如何将正则表达式仅用于 src ?
【问题讨论】: