【问题标题】:Regular Expression to replace iframe src - preg_replace based on size正则表达式替换 iframe src - 基于大小的 preg_replace
【发布时间】: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 ?

【问题讨论】:

    标签: php regex iframe curl


    【解决方案1】:

    怎么样:

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

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

    编辑:

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

    【讨论】:

    • 如果广告是使用 cdn1 而不是 cdn2 加载的呢?
    • @Analog 抱歉,我没有发现这种差异。现在怎么样?
    • 不幸的是,当我使用上面的代码时,它只会破坏页面而没有显示任何错误消息。我认为这可能只是因为我出于某种原因使用了 preg_replace,所以我尝试将原始 str_replace 之一更改为 preg_replace,同时将在线上的其他所有内容保持不变,以查看是否也破坏了页面并且确实如此。但随后仔细检查,我使用了以下 preg_replace 确实有效,但它是一个更简单的正则表达式设置来删除整个 iframe,这不是我的意图 $toremove = preg_replace('//', '', $toremove );
    • @Analog 哦,糟糕,我也忘记了一些重要的事情。我将编辑我的答案。
    • 完美,最后一次改变它的工作方式正是我需要的。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-09-04
    • 2020-10-16
    • 2019-06-03
    • 2016-03-28
    • 2015-02-16
    • 2018-04-07
    • 1970-01-01
    相关资源
    最近更新 更多