x1nick Posted June 21, 2011 Share Posted June 21, 2011 Regex isnt my strong point at all. I am writing a bit of a odd templating system where it replaces special HTML like syntax with PHP code Anyway the different types of syntax can include <fw:head> <fw:var name="test"> <fw:var name="test2" format="max"> <fw:var name="test2" format="max date[d M Y]"> Basically format could have one or two options seperated by a space, but options defined within [ ] can have spaces The name field would never have a space There are some others, but think if I can get the regex for these I can figure the rest out myself. Currently I do the following Seperate out all the <fw: possibilities in a string using preg_match_all('/<fw:(.*)>/Uis', $source, $matches); I then loop through the matches this produces looking for spaces using preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $matches[1][$i], $options); Im either looking to 1) run this all in one regex statment (would that even be possible) 2) work on the second regex to support eg="eg eg" Quote Link to comment https://forums.phpfreaks.com/topic/240007-preg_match_all-html-syntax-for-templates/ Share on other sites More sharing options...
xyph Posted June 21, 2011 Share Posted June 21, 2011 Does order count? IE is this <fw:var name="test2" format="max date[d M Y]"> The same as this <fw:var format="max date[d M Y]" name="test2"> Quote Link to comment https://forums.phpfreaks.com/topic/240007-preg_match_all-html-syntax-for-templates/#findComment-1232910 Share on other sites More sharing options...
x1nick Posted June 21, 2011 Author Share Posted June 21, 2011 They could be in any order really So yes they are both the same Thanks Nick Quote Link to comment https://forums.phpfreaks.com/topic/240007-preg_match_all-html-syntax-for-templates/#findComment-1232921 Share on other sites More sharing options...
xyph Posted June 21, 2011 Share Posted June 21, 2011 Something like this? <?php $expr = ' /<fw:([A-z]++) (?: \ (name|format)=" ([A-Za-z0-9]++)(?:\[([A-Za-z0-9 ]++)\]){0,1} (?:\ ([A-Za-z0-9]++)(?:\[([A-Za-z0-9 ]++)\]){0,1}){0,1} " ){0,1} (?: \ (name|format)=" ([A-Za-z0-9]++)(?:\[([A-Za-z0-9 ]++)\]){0,1} (?:\ ([A-Za-z0-9]++)(?:\[([A-Za-z0-9 ]++)\]){0,1}){0,1} " ){0,1} /x'; $str = '<fw:head> <fw:var name="test"> <fw:var name="test2" format="max[test]"> <fw:var name="max date[test]" format="max date[d M Y]">'; preg_match_all( $expr, $str, $matches, PREG_SET_ORDER ); foreach( $matches as $match ) { echo '<h3>'. htmlentities($match[0]) .'</h3>'; echoBR( 'FW Type: ' . $match[1] ); echoBR( 'Attribute 1: ' .( isset($match[2]) ? $match[2] : '' ) ); echoBR( 'A 1 Value 1: ' .( isset($match[3]) ? $match[3] : '' ) ); echoBR( 'A 1 V 1 Square Brackets: ' .( isset($match[4]) ? $match[4] : '' ) ); echoBR( 'A 1 Value 2: ' .( isset($match[5]) ? $match[5] : '' ) ); echoBR( 'A 1 V 2 Square Brackets: ' .( isset($match[6]) ? $match[6] : '' ) ); echoBR( 'Attribute 2: ' .( isset($match[7]) ? $match[7] : '' ) ); echoBR( 'A 2 Value 1: ' .( isset($match[8]) ? $match[8] : '' ) ); echoBR( 'A 2 V 1 Square Brackets: ' .( isset($match[9]) ? $match[9] : '' ) ); echoBR( 'A 2 Value 2: ' .( isset($match[10]) ? $match[10] : '' ) ); echoBR( 'A 2 V 2 Square Brackets: ' .( isset($match[11]) ? $match[11] : '' ) ); echoBR(); } function echoBR( $str = '' ) { echo $str . '<br>'."\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240007-preg_match_all-html-syntax-for-templates/#findComment-1232950 Share on other sites More sharing options...
x1nick Posted June 21, 2011 Author Share Posted June 21, 2011 wow, first look it looks perfect. I am assuming I can add more of (?:\ ([A-Za-z0-9]++)(?:\[([A-Za-z0-9 ]++)\]){0,1}){0,1} To increase the number of options within A 1 and A 2 for example. It would just simply re arrange the results of $matches? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240007-preg_match_all-html-syntax-for-templates/#findComment-1232960 Share on other sites More sharing options...
xyph Posted June 22, 2011 Share Posted June 22, 2011 Experiment! If you run into issues, post here and I'll reply in a day or two. Any specific parts you don't know, I can explain Quote Link to comment https://forums.phpfreaks.com/topic/240007-preg_match_all-html-syntax-for-templates/#findComment-1233125 Share on other sites More sharing options...
x1nick Posted June 22, 2011 Author Share Posted June 22, 2011 All seems to be working perfectly now Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240007-preg_match_all-html-syntax-for-templates/#findComment-1233297 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.