Reputation: 16835
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.
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!
Upvotes: 0
Views: 1027
Reputation: 8614
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
Upvotes: 1
Reputation: 140
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
}
?>
Upvotes: 1