Jump to content

preg_match_all HTML Syntax for templates


x1nick

Recommended Posts

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"

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";
}

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.