开发者

Question about preg_replace

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.


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 );
}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜