I wish to replace tags by a user defined function.
But only the first match is done. I don't understand why, but I guess it's a regex problem...
template.html:
Code: Select all
<html xmlns:fb="http://www.blackwizzard.com/ftl/1.1">
<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>
</html>Code: Select all
<?php
function freadFile($file) {
if (!$handle = fopen ($file, "r")) {
exit;
}
$contents = fread ($handle, filesize($file)+1);
fclose($handle);
return $contents;
}
$buffer = freadFile("template.html");
// <([A-Z][A-Z0-9]*)[^>]*>(.*?)</\1[:]\2>
// <\<fb:(.*?)\>(.*?)\</fb:message\>>
$output = $buffer;
$output = preg_replace("<\<fb:(.*?) (.*?)\>(.*?)\</fb:message\>>",replace_function('\\1','\\2','\\3'),$output,-1,$count);
echo "<textarea cols=80 rows=20>$count DONE\n\n".$output."</textarea>";
function replace_function($tagType, $tagArgs, $in) {
return "[[type:$tagType args:($tagArgs) $in]]";
}
?>Code: Select all
1 DONE
<html xmlns:fb="http://www.blackwizzard.com/ftl/1.1">
[[type:message args:(title="Hello") world]]
<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>
</html>thx in advance!