HarryG80 Posted September 13, 2009 Share Posted September 13, 2009 Hello, Im trying to build a template system all of its working except my <if> statement variables <?php $c = ' <if conditional="[LOGGED_IN]"> <a href="logout.php">Logout</a> </if> '; preg_match_all('~<if conditional="([^"]+)">([^"]+)</if>~',$c,$matches); print_r($matches); ?> This will not work because i have the character " in the if statement and for some reason (.*) will not work got any tips on what regex i should use to parse this Thanks Link to comment https://forums.phpfreaks.com/topic/174087-solved-html-parsing/ Share on other sites More sharing options...
Garethp Posted September 13, 2009 Share Posted September 13, 2009 The dot modifier doesn't match newlines. So you need to use this preg_match_all('~<if conditional="([^"]+)">(.+)</if>~s',$c,$matches); All I did was put the s modifier on the end, which forces the dot to match all characters, even new lines Link to comment https://forums.phpfreaks.com/topic/174087-solved-html-parsing/#findComment-917683 Share on other sites More sharing options...
HarryG80 Posted September 13, 2009 Author Share Posted September 13, 2009 Thanks that works great but now i have another issue if there is more then 1 <if> statement they dont split up into different arrays code example: <?php $c = ' <if conditional="[LOGGED_IN]"> <a href="logout.php">Logout</a> \' ?? >.../. </if> random data <br /> <if conditional="[RATE]"> You have already rated this person </if> '; preg_match_all('~<if conditional="([^"]+)">(.+)</if>~s',$c,$matches); print_r($matches); ?> Output: Array ( [0] => Array ( [0] => <if conditional="[LOGGED_IN]"> <a href="logout.php">Logout</a> ' ?? >.../. </if> random data <br /> <if conditional="[RATE]"> You have already rated this person </if> ) [1] => Array ( [0] => [LOGGED_IN] ) [2] => Array ( [0] => <a href="logout.php">Logout</a> ' ?? >.../. </if> random data <br /> <if conditional="[RATE]"> You have already rated this person ) ) Thanks Again Link to comment https://forums.phpfreaks.com/topic/174087-solved-html-parsing/#findComment-917686 Share on other sites More sharing options...
thebadbad Posted September 14, 2009 Share Posted September 14, 2009 Add a question mark after the plus to make it ungreedy. Link to comment https://forums.phpfreaks.com/topic/174087-solved-html-parsing/#findComment-918239 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.