I’m attempting to do something very odd, and I’m having some luck, but am running in to some snags. I have basically finished the first bit of it, now I just need to translate it across the board. This has to do with WordPress, but it is a general concept question as apposed to a WP specific, so I decided to ask it here.
Basically, what I’m trying to do is parse through a variable, and replace a bit of code with something else, if it exists (for WP users, I’m attempting to parse through the_content(), a replace a bit based on some conditions).
I am parsing over a variable called $content. Somewhere within $content will be a standard YouTube emebed code <iframe src="http://www.youtube.com/embed/xxxxxxxxx" frameborder="0" width="560" height="315"></iframe> etc. What I am aiming to do, is grab the xxxxxx value, then replace the whole <iframe> with an <img> anchor.
I have the regex to grab the YouTube video id, and can echo the image no problem. I used:
if (preg_match('(/embed/([a-z0-9A-Z]+))', $content, $output) > 0) {
echo("<img src='http://img.youtube.com/vi/".$output[1]."/0.jpg' alt=' ' />");
}
Which returns the thumbnail of the YouTube video found in $content. I am having trouble translating this regex in to a preg_replace so that I can change the <iframe> in to an <img src='http://img.youtube.com/vi/xxxxxxxxx/0.jpg /> instead. Is this possible?
I would love if someone could point me in the right direction.
Thanks!
Tre
I hate doing this so quickly. But, I seem to have solved my issue already.
Coming on here and typing it out and clearing my mind seems to help here and there.
I was able to accomplish it with this code (which I’m sure is ugly):