AP-Richard Posted January 30, 2008 Share Posted January 30, 2008 Hello, First of all - I'm quite new to Regex, but knowing the amount of work it could save me when programming, I thought I should learn it. Well.. I came across a problem quite quickly. First of all, lets get to my code. The Code <?php $xml = "<game id='123'>Testing, 123, 1</game> <game id='456'>Testing, 456, 1</game>"; preg_match_all("/<game id='[0-9]+'>.+<\/game>/", $xml, $matches); print_r($matches); The Output Array ( [0] => Array ( [0] => <game id='123'>Testing, 123, 1</game> <game id='456'>Testing, 456, 1</game> ) ) The Problem Well, as you can see, I expected it, to cut it at </game>, as thats what I put in my regex (/<game id='[0-9]+'>.+<\/game>/), but it just carried on, till it got to the last </game> (I assume?) Can anyone shed some light on this problem? as I'm SURE its something i've done wrong. Thanks, Richard PS. Expected output would be: Array [0] => Array [0] => <game id='123'>Testing, 123, 1</game> [1] => <game id='456'>Testing, 456, 1</game> Thanks Quote Link to comment Share on other sites More sharing options...
AP-Richard Posted January 30, 2008 Author Share Posted January 30, 2008 Hello, Solved. I changed the regex to: /<game id='[0-9]+'>.*?<\/game>/s Quote Link to comment Share on other sites More sharing options...
effigy Posted January 30, 2008 Share Posted January 30, 2008 Do you understand laziness and greediness now? Note that you can change your delimiter to avoid escaping the forward slashes. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.