2

I need to do a preg_replace:

<p><img class="mceItem" src="http://img.youtube.com/vi/PZVfZ9TmW6w/0.jpg" alt="PZVfZ9TmW6w" width="306" height="229" /></p>

and replace it to:

<iframe width="560" height="345" src="http://www.youtube.com/embed/PZVfZ9TmW6w" frameborder="0" allowfullscreen></iframe>

but i'll have alot of:

<p><img class="mceItem" src="http://img.youtube.com/vi/PZVfZ9TmW6w/0.jpg" alt="PZVfZ9TmW6w" width="306" height="229" /></p>

then i need to do a preg_replace to replace all them.

it's possible do to with preg_replace?

thanks.

Darj2h
  • 21
  • 1
  • 1
    Please, don't parse HTML with regex. – Blender Sep 17 '11 at 03:29
  • 1
    Short answer is even you are able to in some specific case you shouldn't and the tradition in questions like this is to link to [this answer](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). – Ray Toal Sep 17 '11 at 03:35

3 Answers3

0

By default preg_replace will replace all the matches it encounters. You can limit the number by adding a parameter.

preg_replace( '/regex/', 'replaceWith', $string, $noOfMatches );

Hope that answers your question, Good luck.

If you're looking for the regex itself then a quick match to that string would be..

'#<p><img class\="mceItem" src\="http:\/\/img\.youtube\.com\/vi\/PZVfZ9TmW6w\/0\.jpg" alt\="PZVfZ9TmW6w" width\="306" height\="229" \/><\/p>#'

I just escaped the special characters. Here is a cheatsheet on regex. If you're parsing HTML a lot, you might want to check into something called DOMDocument in PHP. It lets you manipulate the dom structure and makes the process simple when you learn how to use it.

If you're just starting out I recommend SimpleHTMLDom parser, which comes in a single file to drag onto your server. It's simpler and a good introduction. There are a lot of examples as well. It's maintained here.

Edit: Okay, to get the video id first with regex..

preg_match_all( '#img\.youtube\.com\/vi\/(.*?)\/#', $searchString, $matches );
foreach( $matches[1] as $vid_id )
{
  $iframe_string = '<iframe width="560" height="345" src="http://www.youtube.com/embed/'.$vid_id.'" frameborder="0" allowfullscreen></iframe>';
  preg_replace( '#<p><img class\="mceItem" src\="http:\/\/img\.youtube\.com\/vi\/\'.$vid_id.\'\/0\.jpg" alt\="'.$vid_id.'" width\="306" height\="229" \/><\/p>#', $iframe_string, $searchString );
}
Adam
  • 1,080
  • 1
  • 9
  • 17
  • i know but i need help with the pattern – Darj2h Sep 17 '11 at 03:30
  • Okay one minute. I'll post one that will work for that given string. – Adam Sep 17 '11 at 03:35
  • That's new. Is there only one unique video ID that you're grabbing? – Adam Sep 17 '11 at 03:51
  • it's only a example, i said: 'i'll have alot of them' but i mean with differents alt values... – Darj2h Sep 17 '11 at 03:52
  • Okay edited it again. This code uses regex to pull all of the video ids from the page it then looks for them in the tags in the page and replaces them one by one with the correct one. Let me know how it turns out. I'm off to sleep. I left the /0.jpg at the end of the replace link, is that in all of them? If not, adjust it. – Adam Sep 17 '11 at 04:06
0

is the expresion

<p><img class="mceItem" src="http://img.youtube.com/vi/PZVfZ9TmW6w/0.jpg" alt="PZVfZ9TmW6w" width="306" height="229" /></p>

always regular?? you could use a substr to get everything but the video number PZVfZ9TmW6w out

Daniel
  • 1,321
  • 12
  • 25
0

Doing this kind of conversion using preg_replace or any RegEx for HTML is just a bad idea.

If you're not so much hung up on the function as the results, you can use phpquery or querypath to get all instances within an HTML page and recurse through to replace whatever's needed.

Probably the best article on StackOverflow IMHO that deals with parsing HTML in PHP is this one: How to parse HTML with PHP?

It also talks about why and why not to parse HTML using regular expressions. Mostly why not. For myself and a lot of other Developers it has been nothing but a recipe for pain to try to parse HTML (which may have slight differences from link to link or even from line to line) using regular expressions. HTML is just too loose and permissive a markup to have a RegEx that will work 100% of the time.

Community
  • 1
  • 1
Liam
  • 1,712
  • 1
  • 17
  • 30