繁体   English   中英

正则表达式替换iframe src-基于大小的preg_replace

[英]Regular Expression to replace iframe src - preg_replace based on size

我正在使用使用curl来加载thepiratebay的piratebay反向代理脚本。 它还具有删除/替换广告的选项,但它使用的是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只是用来删除广告,但我创建了自己的变量并添加了变量,现在用我自己的内容替换了广告。 ($排行榜,$左侧,$右侧,$ topsmall)

但是我发现有更多的广告是通过curl加载的,并且也希望替换它们,问题是这组广告没有静态网址,并且在所有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使用正则表达式?

怎么样:

$toremove = preg_replace('~<iframe src="http://cdn[0-9]+\.adexprt\.com[^"]+" width="469" height="60" frameborder="0" scrolling="no"></iframe>~', 'replaced content', $toremove);

[^"]+匹配除双引号外的所有字符。

编辑:

我忘记了定界符。 我在单引号和正则表达式模式中的第一个和最后一个字符之间放置了~

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM