Jump to content

preg replace erasing other words


violinrocker

Recommended Posts

preg_replace( '#width="([^/\.]+)"#', 'width="100%"', $flv);

 

$flv: width="600" height="438" id="veohFlashPlayerEmbed" name="veohFlashPlayerEmbed"></embed></p>

 

preg_replace replace "width="600" " to "width=100%" the problem is... it erases " height="438" id="veohFlashPlayerEmbed" name="veohFlashPlayerEmbed""

Link to comment
https://forums.phpfreaks.com/topic/228604-preg-replace-erasing-other-words/
Share on other sites

Okay so basically what is happening is your negative character class consumes anything that is not a forward slash or a dot, so it matches all the way up to that "/" in the "</embed>" and then it starts backtracking, giving up characters in the match, until it finds the last " it ran into, to satisfy that " you end your pattern with. 

 

Since you are replacing the value of width, and since the value is delimited by quotes, you should make your negative character class match for anything that is not a quote:

 

preg_replace( '#width="([^"]+)"#', 'width="100%"', $flv);

 

This will make it stop matching at the next " it finds.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.