Analog
Analog

Reputation: 201

Regular Expression to replace iframe src - preg_replace based on size

Im using a piratebay reverse proxy script that uses curl to load the thepiratebay. It also has an option to remove/replace the ads but it's using str_replace and i was wondering if there is a better way to do this.

Here is how the current script removes unwanted content

<?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;
}

the str_replace used to just remove the ads but i created my own variables and added them in which now replaces the ads with my own content. ($leaderboard, $leftside, $rightside, $topsmall)

But i have found more ads that are loaded through curl and would like to replace them as well, the problem is that this set of ads do not have static URLs and have the page title as a variable in all the iframe sources as shown below...

<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>

same ad location different page

<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>

again same ad different page

 <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>

You can see the only thing that changes is the sub url cdn and the end part of the src.

So i was thinking of using preg_replace instead of str_replace and try and use a regular expression just for the iframe src and replace based on the width and height.

So something along the lines of the following

$toremove = preg_replace('<iframe src="/regular expression ?/" width="469" height="60" frameborder="0" scrolling="no"></iframe>', 'replaced content', $toremove);

Would this work as well as how can i use a regex just for the src ?

Upvotes: 1

Views: 1582

Answers (1)

Jerry
Jerry

Reputation: 71578

How about:

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

[^"]+ matches all characters except double quotes.

EDIT:

I forgot about the delimiters. I placed ~ in between the single quotes and the first and last characters in the regex pattern.

Upvotes: 1

Related Questions