PHP find and replace html attribute in string - php

I get iframe from some api and i hold this iframe in some var.
I want to search for "height" and change his value to something else. the same with "scrolling".
For example:
<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>
After the php function the iframe will be:
we have change the "height" to 600px and "scrolling" to no
<iframe src="someurl.com" width="540" height="600" scrolling="no" style="border: none;"></iframe>
i have solution with this code:
$iframe = preg_replace('/(<*[^>]*height=)"[^>]+"([^>]*>)/', '\1"600"\2', $iframe);
the problem is that after the "preg_replace" run it remove all html attributes after the "height"
Thanks

You can use DOMDocument for it. Something like this:
function changeIframe($html) {
$dom = new DOMDocument;
$dom->loadHTML($html);
$iframes = $dom->getElementsByTagName('iframe');
if (isset($iframes[0])) {
$iframes[0]->setAttribute('height', '600');
$iframes[0]->setAttribute('scrolling', 'no');
return $dom->saveHTML($iframes[0]);
} else {
return false;
}
}
$html = '<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>';
echo changeIframe($html);
With this method you can modify iframe as you want.
Thanks.

An example as you requested:
$str = '<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>';
$str = preg_replace('/height=[\"\'][0-9]+[\"\']/i', 'height="600"', $str);
$str = preg_replace('/scrolling=[\"\']yes[\"\']/i', 'scrolling="no"', $str);
echo $str; // -> '<iframe src="someurl.com" width="540" height="600" scrolling="no" style="border: none;"></iframe>'

Related

Passing PHP variable into URL

I have the code:
echo '<iframe width="600" height="450" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?q=266%20Orms%20St%20Providence%20RI&key=mykey" allowfullscreen> </iframe>' ;
I would like to pass the variable $street into the url, after q=, such as:
src="https://www.google.com/maps/embed/v1/place?q=$street&key=mykey
The map is showing but the actual street from $street is not being recognized. Furthermore, how would I pass more than 1 variable? I tried passing $street and $city ,unsucessfully:
src="https://www.google.com/maps/embed/v1/place?q=$street+city&key=mykey
you're using single quotes, so you need to do concatenation:
echo '<iframe width="600" height="450" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?q=' . urlencode($street) . '" allowfullscreen> </iframe>';
You can try also this.
echo '<iframe width="600" height="450" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?q='.$street.'" allowfullscreen> </iframe>';

wrapped all unwrapped text with <p>

I have this string:
$str = 'সাংবাদিক<p>দলীয় সূত্রে</p>'
.'<img width="600" src="img/1.jpg">বিলুপ্ত হওয়া পাবনা'
.'বিলুপ্ত হওয়া পাবনা<img width="600" src="img/1.jpg">'
.'বিলুপ্ত হওয়া পাবনা<img width="600" src="img/1.jpg">বিলুপ্ত হওয়া পাবনা'
.'<p>শাহজাদপুর </p>';
and I want to turn into:
$str = '<p>সাংবাদিক</p><p>দলীয় সূত্রে</p>'
.'<img width="600" src="img/1.jpg"><p>বিলুপ্ত হওয়া পাবনা</p>'
.'<p>বিলুপ্ত হওয়া পাবনা</p><img width="600" src="img/1.jpg">'
.'<p>বিলুপ্ত হওয়া পাবনা</p><img width="600" src="img/1.jpg"><p>বিলুপ্ত হওয়া পাবনা</p>'
.'<p>শাহজাদপুর </p>';
I tried regex
$str = preg_replace('/^(?!<p>).*(?!<\/p>)/m', '<p>$0</p>', $str);
but not doing properly. Please help
It isn't a job for regex but for DOMDocument. Since you are working with html parts and not a whole html document, you need to wrap your string into a basic html skeleton to avoid bad surprises with the auto-correction and to provide the document encoding:
$str = 'সাংবাদিক<p>দলীয় সূত্রে</p>'
.'<img width="600" src="img/1.jpg">বিলুপ্ত হওয়া পাবনা'
.'বিলুপ্ত হওয়া পাবনা<img width="600" src="img/1.jpg">'
.'বিলুপ্ত হওয়া পাবনা<img width="600" src="img/1.jpg">বিলুপ্ত হওয়া পাবনা'
.'<p>শাহজাদপুর </p>';
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML('<html><head><meta charset="UTF-8" /></head><body>' . $str . '</body></html>');
$bodyNode = $dom->getElementsByTagName('body')->item(0);
$result = '';
foreach ($bodyNode->childNodes as $childNode) {
$result .= ($childNode->nodeType === XML_TEXT_NODE)
? '<p>' . $dom->saveHTML($childNode) . '</p>'
: $dom->saveHTML($childNode);
}
echo $result;

Why not working the preg replce in iframe?

I would like to embed youtube links, but I also would like to embed the links from ['url'].
I dosen't get any wrong php messange.
My php code is:
$search = '#<iframe(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*<\/iframe>#x';
$replace = '<iframe width="560" height="315" src="http://www.youtube.com/embed/$2" frameborder="0" allowfullscreen></iframe>';
$text = preg_replace($search, $replace, $text);
And the iframe:
echo ' <iframe class="video" allowfullscreen style="overflow-x: hidden; overflow-y: hidden;" width="658px" height="569" frameborder="0" src="'.$row['url'].$text.'"></iframe>' ;
I would like to fix this becouse now if you give a youtube link the output looks like this:
<iframe class="video" allowfullscreen="" style="overflow-x: hidden; overflow-y: hidden;" width="658px" height="569" frameborder="0" src="https://www.youtube.com/watch?v=dh-0jH4Rb7Y"></iframe>
Why don't just use:
$fullUrl = //put here anything you want, be it result from MySQL or $_POST
echo ' <iframe class="video" allowfullscreen style="overflow-x: hidden; overflow-y: hidden;" width="658px" height="569" frameborder="0" src="'.$fullUrl.'"></iframe>' ;
and decide what should $fullUrl be ahead of echoing? For preg_replace to work you can't echo, you need to assign it to variable first. And why you want to preg_replace something that you assign with variable by yourself? Waste of memory and time.

using php inside of iframe html tags

I am trying to create a website that displays another website in an iframe.
This is my code:
<html>
<title>HTML with PHP</title>
<style>
body {
margin: 0px;
}
</style>
<body>
<?php
$page = $_GET['query'];
echo $page;
?>
<iframe align="center" width="100%" height="100%" src="<?=$page;?>" frameborder="yes" scrolling="yes" name="myIframe" id="myIframe"> </iframe>
</body>
</html>
When I open the php file off of my website (using a url website.com/file.php?query=https://www.google.com, and look in the inspector, I can see the page that the iframe loaded, but it just shows up as a blank page, not the . I have it up at
http://www.test.fire-light.tk/web.php?query=url (replace url with any valid url). I am not sure why it shows the blank page.
Using Iframe you can call PHP file in HTML file . And write your PHP code in test.php Iframe code <iframe src="test.php"></iframe>
I think this:
<iframe align="center" width="100%" height="100%" src="<?=$page;?>"
frameborder="yes" scrolling="yes" name="myIframe" id="myIframe"> </iframe>
should be changed into:
<iframe align="center" width="100%" height="100%" src="<? echo $page; ?>"
frameborder="yes" scrolling="yes" name="myIframe" id="myIframe"> </iframe>
Try this instead:
<html>
<title>HTML with PHP</title>
<style>
body {
margin: 0px;
}
</style>
<body>
<?php
$url = $_SERVER['REQUEST_URI'];
$parts = explode('?=', $url);
$page = $parts[1];
echo '<iframe align="center" width="100%" height="100%" src="$page" frameborder="yes" scrolling="yes" name="myIframe" id="myIframe"> </iframe>';
?>
</body>
</html>

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

Categories