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.
Related
I am trying to use if in an echo with the ternary operator, but instead of a value, when if turns true it should display an iframe
echo(...'.((!$artArr[$count]["Longitude"] == null &&
!$artArr[$count]["Latitude"] == null)?<iframe width="425" height="350"
frameborder="0" scrolling="no" marginheight="0" marginwidth="0"
src="https://www.openstreetmap.org/export/embed.html?bbox='.$artArr[$count]["Longitude"].'%2C'.$artArr[$count]["Latitude"].'%2C'.$artArr[$count]["Longitude"].'%2C'.$artArr[$count]["Latitude"].'&layer=mapnik&marker='.$artArr[$count]["Latitude"].'%2C'.$artArr[$count]["Longitude"].'"
style="border: 1px solid black">:.')
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>';
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>'
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.
i am trying to find a way to get the tag from a string and remove the style attribute .
After that i want to add my own style and keep the following text..
For example, i have:
<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p>
end the endresult should be:
<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 100%;" /></p><p> </p><p>
I have unsuccesfully tried regex but it seems like i am too dumb to understand its functionality...
Every help would be appreciated!
Greetings from Greece
Jim
You can do it by regex like that:
$str = '<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p>';
$newStr = preg_replace('#<img (.+) style="(.+)" />#isU', '<img $1 style="width: 100%" />', $str);
To remove height or some other property:
$string = '<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p' ;
$pattern = "/height:\s*\d*\s*(px|%);*/" ;
$new = preg_replace($pattern,"", $string) ;
echo htmlentities($new) ;
Or remove all style things and replace with own ones:
$string = '<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p' ;
$pattern = "/style=[\'\"][^\"|^\']*[\'\"]/" ;
$own_style = "style='width: 50%'" ;
$new = preg_replace($pattern, $own_style, $string) ;
echo htmlentities($new) ;
Generally using RegExp on HTML tags is kinda bad thing and should be avoided, but in some situations may be applicable.