Joesavage Posted September 12, 2008 Share Posted September 12, 2008 To start: im not good with regex. All i want to do is capture all of the information inbetween <!-- Begin Description --> and <!-- End Description -->. In the example i have below i only have 'k' and i cant even get that to work. In real life i want to be able to capture text with new lines and white text and other weird characters. $data = "<!-- Begin Description -->k<!-- End Description -->"; $regex = '/"<!-- Begin Description -->(.+?)<!-- End Description -->/'; preg_match_all($regex,$data,$description,PREG_SET_ORDER); print_r ($description); The description array gets printed out empty. Any ideas why? I have tried using (.+?), (.*), and (.*?). These are all snippets that i have seen used other places. Quote Link to comment https://forums.phpfreaks.com/topic/123994-solved-preg_match-difficulties/ Share on other sites More sharing options...
effigy Posted September 12, 2008 Share Posted September 12, 2008 You have a stray double quote in your pattern. To match new lines you need the /s modifier: /<!-- Begin Description -->(.+?)<!-- End Description -->/s Quote Link to comment https://forums.phpfreaks.com/topic/123994-solved-preg_match-difficulties/#findComment-640100 Share on other sites More sharing options...
DarkWater Posted September 12, 2008 Share Posted September 12, 2008 Code: <?php $data = "<!-- Begin Description -->k<!-- End Description -->"; $regex = '/\Q<!-- Begin Description -->\E(.+?)\Q<!-- End Description -->\E/s'; preg_match_all($regex,$data,$description); print_r ($description); Output: Array ( [0] => Array ( [0] => <!-- Begin Description -->k<!-- End Description --> ) [1] => Array ( [0] => k ) ) $description[1] will be a multi-dim array of the matches in between the tags. EDIT: I don't know why I just used \Q...\E, but whatever. Doesn't matter. Makes it easier to change it in the future maybe? *shifty eyes* Quote Link to comment https://forums.phpfreaks.com/topic/123994-solved-preg_match-difficulties/#findComment-640101 Share on other sites More sharing options...
effigy Posted September 12, 2008 Share Posted September 12, 2008 EDIT: I don't know why I just used \Q...\E, but whatever. Doesn't matter. Makes it easier to change it in the future maybe? *shifty eyes* It does matter. If it's not required, don't do it. Otherwise, you'll have the next coder wondering what the intention was. Quote Link to comment https://forums.phpfreaks.com/topic/123994-solved-preg_match-difficulties/#findComment-640105 Share on other sites More sharing options...
Joesavage Posted September 12, 2008 Author Share Posted September 12, 2008 damn... /s makes it work perfectly. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/123994-solved-preg_match-difficulties/#findComment-640114 Share on other sites More sharing options...
DarkWater Posted September 12, 2008 Share Posted September 12, 2008 damn... /s makes it work perfectly. Thank you very much. Actually, the real reason it was failing was because of the " in front of the expression. The s modifier is to make . match \n as well as every character. @effigy: That's what comments are for. That's beside the point. I didn't feel like going and taking it out, so I just added that comment at the bottom. =P Quote Link to comment https://forums.phpfreaks.com/topic/123994-solved-preg_match-difficulties/#findComment-640121 Share on other sites More sharing options...
effigy Posted September 12, 2008 Share Posted September 12, 2008 @effigy: That's what comments are for. That's beside the point. I didn't feel like going and taking it out, so I just added that comment at the bottom. =P No. Comments are not for worthless code additions, but for seemingly confusing and/or complex ones. You decided to add 140 characters to your post rather than remove 8? Interesting. Quote Link to comment https://forums.phpfreaks.com/topic/123994-solved-preg_match-difficulties/#findComment-640138 Share on other sites More sharing options...
DarkWater Posted September 12, 2008 Share Posted September 12, 2008 Wow, that was 140 characters? Never mind then, lol. I type faster than I delete, so I just typed. Whatever. xD Quote Link to comment https://forums.phpfreaks.com/topic/123994-solved-preg_match-difficulties/#findComment-640158 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.