bugroff licensed tree converter
| From: | alvarez at sto13 dot st-oneline dot de | Date: | Wed, 29 Aug 2001 08:15:21 +0000 |
| Subject: | bugroff licensed tree converter | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-64954@lists.php.net to get a copy of this message | ||
I've written a simple recursive function to convert trees
like <*a* <*b*, *c*>, <*d*>>
into PHP arrays like the corresponding one as generated by
array (a, array (b, c), d)
The resulting array construct is directly usable. Feel free to
include the function in your own sources and modify it at will.
I hereby release it under the bugroff license available from
http://www.geocities.com/SoHo/Cafe/5947/bugroff.html
this is the code:
function free_to_array ($str) {
$i = 0;
while ($cursor < strlen ($str)) {
while ( ($c = substr ($str, $cursor, 1)) == " "
|| $c == "\n" || $c == "," || $c == ".")
$cursor ++;
$match = $c;
if ($c == "<")
$match = ">";
$end = strpos ($str, $match, ++ $cursor);
$level[$i] = substr ($str, $cursor, $end - $cursor);
if ($c == "<")
$level[$i] = free_to_array ($level[$i]);
$cursor = $end + 1;
$i ++;
}
return $level;
}
and a practical application example:
xml_open ("test", "<*language*> <*JavaScript*>");
creating element/value pairs for use in XML-like documents
without the syntax overhead of nested calls to arrays.
D. Alvarez Arribas <alvarez@st-oneline.net>
>You have to write your own function for this.
>> I want a function or macro to return executable code yielding syntax
>> preprocessing of binary trees. Because the Macro Processor seems to be
>> somewhat messy I though a regular function might be capable of doing the
>> job.
>>
>> e.g.
>>
>> btree ('(("a", "b")("c", "d"))')
>>
>> should be expanded to
>>
>> array (array ("a", "b")
>> array ("c", "d")
>>
>>
>> How can I archieve this?