premiso Posted December 5, 2008 Share Posted December 5, 2008 So I am learning a bit of regex using preg, and I have this code I am trying to figure out how to parse more efficiently and for reasons I cannot find the definition to so I can fix it thus I am asking here. <?php $string = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="400" height="300"><param name="src" value="../bcms_uploads/myVideo.flv" /><param name="width" value="400" /><param name="height" value="300" /><embed type="application/x-shockwave-flash" src="../bcms_uploads/myVideo.flv" width="400" height="300"></embed></object>'; $html=preg_replace('~<object.*src="(.*)["](.*)</object>~','<a href="$1" style="display:block;width:400px;height:300px" id="player">df</a>',$string); echo $html . " HTMLs"; ?> The printed code is: <a href="../bcms_uploads/myVideo.flv" width="400" height="300" style="display:block;width:400px;height:300px" id="player">df</a> HTMLs What it seems to be doing is finding the last " before it starts finding other stuff, I am not sure why it displays the width and height and not other information, but how do I get it to just stop after the first quote instead of finding some others first then stopping? I hope what I said made sense and that it is not too hard of a question to answer. I know there are other ways to achieve the results, but I would just like to know why this is happening and if there is a way to do it with the above code. Note: width="400" height="300" Shouldn't be in the end result. Link to comment https://forums.phpfreaks.com/topic/135714-solved-preg_replace-how-to-avoidstop-this/ Share on other sites More sharing options...
effigy Posted December 5, 2008 Share Posted December 5, 2008 Quantifiers are greedy by default; make the quantifier lazy (example) or the condition more specific: ~<object.*src="([^"]+)".*</object>~is. Link to comment https://forums.phpfreaks.com/topic/135714-solved-preg_replace-how-to-avoidstop-this/#findComment-707151 Share on other sites More sharing options...
premiso Posted December 5, 2008 Author Share Posted December 5, 2008 Awesome thanks man. Link to comment https://forums.phpfreaks.com/topic/135714-solved-preg_replace-how-to-avoidstop-this/#findComment-707157 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.