Analysing bbcode-like code
Posted: Mon Jan 19, 2009 2:50 pm
Hello,
Here is a code in a home-made language, created to be used as a template.
My template parser is working fine, except for the resources it uses.
Here is how it works:
It load the template file, and create a "map" locating the tags with many details, allowing the rest of the template parser to access easily the tag doing replaces, analysis, and everything needed.
But creating this map is taking a lot of resources since i'm not using any regex.
Here is an example of the code:
And here is the map generated, output via print_r():
QUestion:
How can I create this kind of "map" in an array with regex?
Here is a code in a home-made language, created to be used as a template.
My template parser is working fine, except for the resources it uses.
Here is how it works:
It load the template file, and create a "map" locating the tags with many details, allowing the rest of the template parser to access easily the tag doing replaces, analysis, and everything needed.
But creating this map is taking a lot of resources since i'm not using any regex.
Here is an example of the code:
Code: Select all
<fb:message title="Hello">world</fb:message>
<fb:message title="another message" message="another content" />
<fb:box title="this is the box's title" width="80%">
box's content
<fb:message title="hello">
a message in a box
</fb:message>
</fb:box>
Code: Select all
Array
(
[0] => Array
(
[name] => message
[args] => Array
(
[title] => Hello
)
[str_args] => title="Hello"
[start] => 124
[end] => 150
[len] => 26
[str] => <fb:message title="Hello">
[isSingleTag] => 0
[type] => start
)
[1] => Array
(
[name] => message
[args] => Array
(
)
[str_args] =>
[start] => 155
[end] => 168
[len] => 13
[str] => </fb:message>
[isSingleTag] => 0
[type] => end
)
[2] => Array
(
[name] => message
[args] => Array
(
[title] => another message
[message] => another content
)
[str_args] => title="another message" message="another content" /
[start] => 174
[end] => 238
[len] => 64
[str] => <fb:message title="another message" message="another content" />
[isSingleTag] => 1
[type] => start
)
[3] => Array
(
[name] => message
[original_name] => message
[args] => Array
(
)
[str_args] =>
[start] => 238
[end] => 238
[len] => 0
[str] =>
[isSingleTag] => 1
[type] => end
)
[4] => Array
(
[name] => box
[args] => Array
(
[title] => this is the box's title
[width] => 80%
)
[str_args] => title="this is the box's title" width="80%"
[start] => 244
[end] => 296
[len] => 52
[str] => <fb:box title="this is the box's title" width="80%">
[isSingleTag] => 0
[type] => start
)
[5] => Array
(
[name] => message
[args] => Array
(
[title] => hello
)
[str_args] => title="hello"
[start] => 323
[end] => 349
[len] => 26
[str] => <fb:message title="hello">
[isSingleTag] => 0
[type] => start
)
[6] => Array
(
[name] => message
[args] => Array
(
)
[str_args] =>
[start] => 382
[end] => 395
[len] => 13
[str] => </fb:message>
[isSingleTag] => 0
[type] => end
)
[7] => Array
(
[name] => box
[args] => Array
(
)
[str_args] =>
[start] => 401
[end] => 410
[len] => 9
[str] => </fb:box>
[isSingleTag] => 0
[type] => end
)
)
How can I create this kind of "map" in an array with regex?