HTML string append/replace in PHP - php

I have the following html string in my php code:
<iframe src="https://player.vimeo.com/video/123456789" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
I want to append parameters to the URL give in the src attribute. The result should look as follows:
<iframe src="https://player.vimeo.com/video/123456789/APPENDAGE" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
As you can see, in my example the string /APPENDAGE should be appended to the URL givein in the src attribute.
Usually I would do something like this with a string-replace method, however, in the given case this approach won't work. What would be an efficient approach to reach my goal?

If you don't mind using regex, there is a simple solution for this: (DEMO)
$str = '<iframe src="https://player.vimeo.com/video/123456789" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
';
$replaced = preg_replace('/(?<=src=")(.*?)(?=")/', '$1/APPENDAGE', $str);
var_dump($replaced);
Output:
string '<iframe src="https://player.vimeo.com/video/123456789/APPENDAGE" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
' (length=215)
This will simply match the url, insdie src attr and append /APPENDAGE to it, you can change it to whatever you want.

if you don't mind a large code
<?php
$str = '<iframe src="https://player.vimeo.com/video/123456789" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>';
$obj = simplexml_load_string($str);
//print_r($obj);
$obj['src'] = $obj['src'].'/APPENDAGE';
$s = $obj->asXML();
print(substr( $s, strpos($s, "\n")+1 ));
But this code will also work for rest of the attributes

Related

How to get all YouTube iframe from HTML using regex

I want to get all YouTube iframe using regex and want to add specific tag to each record found.
For example <youtube-frame></youtube-frame> to iframe begeing and end.
Needed output:
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe></youtube-frame>
<youtube-frame><iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe></youtube-frame>
My Code
$embed = '
<iframe width="560" height="315" src="https://www.youtube.com/embed/vakfMRyjulw" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folderp2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/aDGWMlKPKDs" frameborder="0" allowfullscreen></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
<iframe width="600" height="350" src="https://tune.pk/player/embed_player.php?vid=6508414&folder=2016/05/04/&width=600&height=350&autoplay=no" frameborder="0" allowfullscreen scrolling="no"></iframe>
';
What I have tried?
$pattern = '/<iframe\.*src=\"//youtube"\.*/';
$iframeSrc = preg_match($pattern, $embed, $matches);
var_dump($iframeSrc);
Try this:
$iframeSrc = preg_replace('/<iframe[^>]*src\s*=\s*"?https?:\/\/[^\s"\/]*youtube.com(?:\/[^\s"]*)?"?[^>]*>.*?<\/iframe>/i', '<youtube-frame>$0</youtube-frame>', $embed);
This uses preg_replace and a global regex to replace all YouTube IFrame tags (including their closing tags) with <youtube-frame>$0</youtube-frame> where $0 is the original string.
The regex could theoretically be simplified if you are totally sure about the format of your input, but I designed it to be robust enough to cope with other syntaxes such as src=http://example.com or src = "http://example.com" etc. which are accepted by browsers nowadays, and it only matches sources on *.youtube.com domains and not something like myyoutubesite.com.

php preg_replace Replace specific string with specific valus

I studied following example
$copy_date = "Copyright 1999";
$copy_date = preg_replace("([0-9]+)", "2000", $copy_date);
here any numeric will be replaced by 2000
But in following example I am confused !!
How to replace
width="anything" with value 280
Want to Substitute anything that appears after
width="
with
width="280"
example
width="481"
will be width="280"
Another example.....
<iframe width="680" height="480" src="//www.youtube.com/embed/RTcgXcz-_G0" frameborder="0" allowfullscreen></iframe>
after preg_replace
should become
<iframe width="280" height="200" src="//www.youtube.com/embed/RTcgXcz-_G0" frameborder="0" allowfullscreen></iframe>
use this:
$copy_date = '<iframe width="680" height="480" src="//www.youtube.com/embed/RTcgXcz-_G0" frameborder="0" allowfullscreen></iframe>' ;
$pattern = '/width="\d+"/i' ;
$new_style = 'width="280"' ;
$new_copy_date = preg_replace($pattern, $new_style, $copy_date) ;
echo $new_copy_date;

regex replace iframe/object by youtube video id

I have the html like below. Basically it has three videos some of them embed by iframe tag other object tag. I have the YouTube video id. So I want to replace the iframe/object tag with some text (Video can't be showed here ) by YouTube video id.
MY HTML
<p>Video 1</p>
<p><iframe width="604" height="453" src="http://www.youtube.com/embed/TOsGAxFcYls?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>Video 2</p>
<p><iframe width="604" height="340" src="http://www.youtube.com/embed/Y-AYC3_DbpY?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>Video 3</p>
<object width="560" height="315"><param name="movie" value="//www.youtube.com/v/-1jKtYuXkrQ?version=3&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="//www.youtube.com/v/-1jKtYuXkrQ?version=3&hl=en_US" type="application/x-shockwave-flash" width="560" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>
Now, I want to replace the replace the video 1 and 3. I have both video id's.
video 1 = TOsGAxFcYls
video 3 = -1jKtYuXkrQ
Now, I want to replace both iframe and object by particular text.
Expected output
<p>Video 1</p>
<p><strong>Video 1 has been removed video id (TOsGAxFcYls)</strong></p>
<p>Video 2</p>
<p><iframe width="604" height="340" src="http://www.youtube.com/embed/Y-AYC3_DbpY?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>Video 3</p>
<strong>Video 3 has been removed video id (-1jKtYuXkrQ)</strong>
Note : I may add custom replacement text for each replacement of
video.
please help me out with regular expression to do the above job!
Here is a simple solution, just add the ID's and their replacement texts to the $video_ids array.
$html= ... // your html here
$video_ids = array
(
array("TOsGAxFcYls","First replacement text"),
array("Y-AYC3_DbpY","Second replacement text")
);
foreach ($video_ids as &$video_id) {
$patt = "/<object(.*)$video_id[0](.*)<\/object>/";
$html = preg_replace($patt, $video_id[1], $html);
$patt = "/<iframe.*?src\=".*?'.$video_id[0].'.*.<\/iframe>/i";
$html = preg_replace($patt, $video_id[1], $html);
}
echo $html; // here are your changed values
Here is a sample code that I believe has the effect your asking for:
<?php
$text = '<iframe width="604" height="340" src="http://www.youtube.com/embed/Y-AYC3_DbpY?feature=oembed" frameborder="0" allowfullscreen></iframe>';
$matches = array();
$video_id = "Y-AYC3_DbpY";
preg_match('/<iframe.*?src\=".*?'.$video_id.'.*.<\/iframe>/i', $text, $matches);
if(!empty($matches)){
//replace iframe;
}
else{
//do something else
}
?>

Extract src from google maps embed link using regex

Hello everybody and thanks for reading.
I have a link like this :
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?f=q&source=s_q&hl=en&geocode=&q=monte+rosa&aq=&sll=45.454082,9.213138&sspn=0.009016,0.01929&t=h&gl=it&ie=UTF8&hq=&hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&ll=45.690627,8.824349&spn=0.008978,0.01929&z=14&iwloc=A&output=embed"></iframe><br /><small>View Larger Map</small>
i want to extract only this
https://maps.google.it/maps?f=q&source=s_q&hl=en&geocode=&q=monte+rosa&aq=&sll=45.454082,9.213138&sspn=0.009016,0.01929&t=h&gl=it&ie=UTF8&hq=&hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&ll=45.690627,8.824349&spn=0.008978,0.01929&z=14&iwloc=A&output=embed
In other words i want to extract everything after the src=" until the end of the link ".
I have been trying with the use of regex but i cant figure out the correct syntax. Some help would be most appreciated.
$html = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?f=q&source=s_q&hl=en&geocode=&q=monte+rosa&aq=&sll=45.454082,9.213138&sspn=0.009016,0.01929&t=h&gl=it&ie=UTF8&hq=&hnear=Monte+Rosa,+Province+of+Varese,+Lombardy&ll=45.690627,8.824349&spn=0.008978,0.01929&z=14&iwloc=A&output=embed"></iframe><br /><small>View Larger Map</small>';
preg_match('~iframe.*src="([^"]*)"~', $html, $result);
var_dump($result[1]);

How to replace width and height an iframe html tag using php?

How to change the width/height if I have the following code and using php:
<iframe src="http://player.vimeo.com/video/43598400"
webkitallowfullscreen=""
mozallowfullscreen=""
allowfullscreen=""
frameborder="0"
height="300"
width="500">
</iframe>
To:
<iframe src="http://player.vimeo.com/video/43598400"
webkitallowfullscreen=""
mozallowfullscreen=""
allowfullscreen=""
frameborder="0"
height="400"
width="680">
</iframe>
Usage is very limited, I want a simple replace solution.
something like this:
$iframe = '<iframe src="http://player.vimeo.com/video/43598400" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="" frameborder="0" height="300" width="500"></iframe>';
$newWidth = 680;
$newHeight = 400;
$iframe = str_replace('height="300"', 'height="' . $newHeight . '"', $iframe);
$iframe = str_replace('width="500"', 'width="' . $newWidth . '"', $iframe);
assuming you are certain that the original width and height will always be 500*300. if not you can try with preg_match()
assuming the variable that holds you string is called $iframe, try with
str_replace('300', '400', $iframe);
str_replace('500', '680', $iframe);
This is, of course, the most hard coded possible solution - show us more of the context in which this iframe code reside if this doesn't help you

Categories