php trim away undesired texts - php

I have the following data in mysql database which is an iframe from u-tube:-
`<iframe width="560" height="315" src="http://www.youtube.com/embed/Om2fabTIKE4" frameborder="0" allowfullscreen></iframe>`
Nevertheless, in the php codes, a present phase is present for the above iframe, in which I just need Om2fabTIKE4 as the variable to be filled in.
I would like to ask, is there any way that I can trim away
<iframe width="560" height="315" src="http://www.youtube.com/embed/
and
frameborder="0" allowfullscreen></iframe>

Try this:
$html = '<iframe width="560" height="315" src="http://www.youtube.com/embed/Om2fabTIKE4" frameborder="0" allowfullscreen></iframe>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$tags = $dom->getElementsByTagName('iframe');
foreach ($tags as $tag)
$link = explode('/',parse_url($tag->getAttribute('src'),PHP_URL_PATH));
var_dump($link[2]);
You can learn more about the DOMDocument class here.

Use str_replace to remove the text

Related

How to get html code from Wordpress post content?

I want to wrap decode all html tags from a post content in Wordpress.
like
<iframe src="https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2FRocketsAreCool%2Fvideos%2F1032994553496742%2F&show_text=0&width=560" width="560" height="315" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" allowFullScreen="true">
When I try this code:
$query = new WP_Query(array('name'=>'example_topic'));
$post = $query->posts;
$text = htmlspecialchars_decode($post->content);
It works, but not closing the html tag because the tag was not originally closed in the encoded tags, so every thing comes after not showing in browser.

PHP array ouput shows only one element

I am working on a small PHP script that will take an array of youtube id's and generate an on the fly youtube embeded playlist.
The current PHP code I am using - only echos ONE video.
<iframe width="560" height="315" src="http://www.youtube.com/embed/182oUgBfoLE?&rel=0&modestbranding=1&hd=1&autohide=1&playlist=
<?php
$videos = Array("PGNiXGX2nLU", "VUJOJ0d7e8c", "o0syTUu3_S0", "rXndd78C8-c" );
// Shuffle array
shuffle($videos);
// Loop array
foreach($videos as $video);
{
// Echo array with commas between elements
echo "$video ,";
}
?>
" frameborder="0" allowfullscreen></iframe>
My question is how can I get the all elements of the shuffled and looped array to be echoed ?
My take
<?php
$videoIds = array("PGNiXGX2nLU", "VUJOJ0d7e8c", "o0syTUu3_S0", "rXndd78C8-c");
shuffle($videoIds);
$playlist = implode(',', $videoIds);
?>
<iframe width="560" height="315"
src="http://www.youtube.com/embed/182oUgBfoLE?&rel=0&modestbranding=1&hd=1&autohide=1&playlist=<?= $playlist; ?>"
frameborder="0" allowfullscreen>
</iframe>
Try using this code -
<?php
$videos = Array ( "PGNiXGX2nLU", "VUJOJ0d7e8c", "o0syTUu3_S0", "rXndd78C8-c" );
// Shuffle array
shuffle($videos);
$playlist = implode(',', $videos);
?>
<iframe width="560" height="315" src="http://www.youtube.com/embed/182oUgBfoLE?&rel=0&modestbranding=1&hd=1&autohide=1&playlist=<?php echo $playlist;?>" frameborder="0" allowfullscreen></iframe>

Regex to remove iframe with facebook but keeps youtube

I want to remove only iframe(and everyhing inside iframe)with facebook like above but to keep youtube iframe:
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.example.com%2F%3Fp%313098&layout=standard&show_faces=true&width=500&action=recommend&colorscheme=light" ></iframe>
To keep iframes from youtube:
<iframe width="640" height="360" src="https://www.youtube.com/embed/hiYtWYLEjlI?rel=0" frameborder="0" allowfullscreen></iframe>
I've this regex but it only remove
<\/*i(?:frame|layer)|l(?:ayer|ink)[^>]*+>
https://regex101.com/r/eM9eS3/5
Better take the xpath approach:
$xml = simplexml_load_string($your_html_string);
$iframes = $xml->xpath("//iframe[contains(#src, 'facebook.com')]");
And delete these:
for ($i=0;$i<count($iframes);$i++) {
$iframe = $iframes[$i];
unset($iframe[0][0]);
}
Your new XML looks like:
echo $xml->asXML();
As whole function:
function goAwayFacebook($html) {
$xml = simplexml_load_string($html);
$iframes = $xml->xpath("//iframe[contains(#src, 'facebook.com')]");
for ($i=0;$i<count($iframes);$i++) {
$iframe = $iframes[$i];
unset($iframe[0][0]);
}
return $xml->asXML();
}
$newhtml = goAwayFacebook($html);
So you are roughly trying to check if www.facebook.com is present in <ifram> or not. This can be achieved by using following regex.
Regex: (?=.*www\.facebook\.com.*)<iframe .*<\/iframe>
Explanation:
(?=.*www\.facebook\.com.*) checks for presence of www.facebook.com between the <iframe> tags.
Regex101 Demo

php regex to change the value html attribute

I have a html string of iframe where width and it's value is included. I want to replace the width's value by regex in php. For example, I am getting a value dynamically as
<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>
I want to change the value of width by the regular expression. Can you help me someone.
Avoid using RegEx in XML/HTML documents, there are a performant libraries to do that, unless there a very very very good reason for that
Try with this code to achieve your job
<?php
$html = '<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>';
$doc = new DOMDocument();
$doc->loadHTML($html);
$elements = $doc->getElementsByTagName('iframe');
foreach($elements as $el) {
$el->setAttribute('width', '1024');
}
print $doc->saveHTML();
OUTPUT
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><iframe width="1024" height="315" src="" frameborder="0" allowfullscreen></iframe></body></html>
sounds like a really bad idea, but here goes... something like
<?php
header('content-type:text/plain;charset=utf8');
$str=base64_decode('PGlmcmFtZSB3aWR0aD0iNTYwIiBoZWlnaHQ9IjMxNSIgc3JjPSIiIGZyYW1lYm9yZGVyPSIwIiBhbGxvd2Z1bGxzY3JlZW4+PC9pZnJhbWU+');
$ret=preg_replace('/(\<iframe.*?width\=)\"(.*?)\"/','${1}"999"',$str);
var_dump($str,$ret);
will change width to 999... but you should really use a proper HTML parser instead, like DOMDocument.
$domd=#DOMDocument::loadHTML($str);
$domd->getElementsByTagName('iframe')->item(0)->setAttribute('width',999);
echo $domd->saveHTML($domd->getElementsByTagName('iframe')->item(0));
will also change width to 999, and is much more reliable (for example, the regex will break if there is spaces or newlines between the width and = , although it would still be legal html.. sigh)

Add "wmode" parameter to src of an iframe with PHP

I have had some problems with the z-index of a FLASH- and an overlaying DIV-element.
I've used this jQuery/Javascript solution, which adds the "wmode=transparent" parameter to the "src" of every (YouTube & Vimeo) iframe, to solve the z-index issues (e.g. flickering, etc).
...
content.find('iframe').each(function() {
var iframe_source = $(this).attr('src');
var iframe_wmode = "wmode=transparent";
if ( iframe_source.indexOf('?') != -1 )
{
iframe_source = iframe_source.split('?');
$(this).attr('src',iframe_source[0]+'?'+iframe_wmode+'&'+iframe_source[1]);
}
else
{
$(this).attr('src',iframe_source+'?'+iframe_wmode);
}
});
...
Now I need this solution in PHP, because I still have some z-index-flickering (during the rendering of the DOM) until the jQuery/Javascript solution corrects this problem ( on $(window).load(function(){} ... $(document).ready(function(){} is not possible for me).
My PHP content looks like this for example ...
...
$content = '
foo bar foo bar
<iframe width="1280" height="720" src="http://www.youtube.com/embed/GASFa7rkLtM" frameborder="0" allowfullscreen></iframe>
foo bar foo bar
<iframe width="100" height="100" src="http://www.youtube.com/embed/GASFa7rkLtM?rel=0" frameborder="0" allowfullscreen></iframe>
foo bar foo bar
<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/GASFa7rkLtM" frameborder="0" allowfullscreen></iframe>
foo bar foo bar
<iframe src="http://player.vimeo.com/video/57959739?autoplay=1&loop=1" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
foo bar foo bar';
...
... and shoud look like this after some preg_match/regex-magic ;)
...
$content = '
foo bar foo bar
<iframe width="1280" height="720" src="http://www.youtube.com/embed/GASFa7rkLtM?wmode=transparent" frameborder="0" allowfullscreen></iframe>
foo bar foo bar
<iframe width="100" height="100" src="http://www.youtube.com/embed/GASFa7rkLtM?rel=0&wmode=transparent" frameborder="0" allowfullscreen></iframe>
foo bar foo bar
<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/GASFa7rkLtM?wmode=transparent" frameborder="0" allowfullscreen></iframe>
foo bar foo bar
<iframe src="http://player.vimeo.com/video/57959739?autoplay=1&loop=1&wmode=transparent" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
foo bar foo bar';
...
Many thanks in advance!
Best Mike =)
PS:
My idea is to solve the z-index problem via PHP in advance (server-side, not client-side).
PPS:
FYI - I get the "content" string with HTML-content/-tags out of a MySQL-DB, and I want to modify these string, instead of modifieng the DOM via jQuery/Javascript.
UPDATE/EDIT:
Buliding on the regex-solution from "One Trick Pony" worked for me. I edited the first and add a second "preg_replace". The first one adds "?wmode=transparent" to the end of each iframe-src and the second replaces the "?" with "&" if exists twice in the url.
$content = preg_replace('#\<iframe(.*?)\ssrc\=\"(.*?)\"(.*?)\>#i',
'<iframe$1 src="$2?wmode=transparent"$3>', $content);
$content = preg_replace('#\<iframe(.*?)\ssrc\=\"(.*?)\?(.*?)\?(.*?)\"(.*?)\>#i',
'<iframe$1 src="$2?$3&$4"$5>', $content);
Not a beautiful solution, but it worked perfect for my purpose.
Any better suggestions?
Using DomDocument:
$dom = new DomDocument();
$dom->loadHtml($content);
foreach($dom->getElementsByTagName('iframe') as $ifr){
// use parse_url here, change query and rebuild it if you want to be 100% sure
$src = rtrim($ifr->getAttribute('src'), '&') . '&wmode=transparent';
$ifr->setAttribute('src', $src);
}
$content = $dom->saveHtml();
A basic try with regular expressions using greedy matches:
$content = preg_replace('#\<iframe(.*?)\ssrc\=\"(.*?)\"(.*?)\>#i',
'<iframe$1 src="$2&wmode=transparent"$3>', $content);

Categories