Page 1 of 1

[SOLVED] Recognising multi-dim arrays in regexp

Posted: Sat Apr 23, 2005 2:18 pm
by Bennettman
Hi, I'm trying to do a preg_replace for multi-dimensional arrays:

<=array[k1][k2]> would be replaced by $array['k1']['k2'] etc - basically <= replaces $ and there's an extra > at the end.

What I have currently is:

Code: Select all

$index_content = preg_replace("/<=([_a-z0-9]+)\[([_a-z0-9]+)\]>/ie", "$$1[$2]", $index_content);
for a 1-dim array, but I'm certain that it could be modified to handle any type of variable or array.

I'm trying this regexp:

Code: Select all

/&lt;=(&#1111;_a-z0-9]+)(\&#1111;&#1111;_a-z0-9]\]*)&gt;/ie
but it returns an "unexpected less-than or equal sign" error. I have a feeling it's related to the brackets being in the parentheses.

Any ideas what I can do to make it work?

Posted: Sat Apr 23, 2005 7:17 pm
by Chris Corbyn
Firstly I'm baffled by your "multi-dimensional array".

Secondly, that error sounds strangely like you haven't put quotes around the regex.

Could you elaborate further on what you're trying to achieve? :)

Posted: Sat Apr 23, 2005 8:16 pm
by Bennettman
Right, what I have is page content (in $index_content) which in places contains code for referencing variables, arrays etc, for example:

Code: Select all

There are &lt;=member_count&gt; members
and I have a preg_replace replace the tag with the value of $member_count at the time the preg_replace is executed.

The current code for variables and arrays is:

Code: Select all

$index_content = preg_replace("/<=([_a-z0-9]+)>/ie", "$$1", $index_content);
$index_content = preg_replace("/<=([_a-z0-9]+)\[([_a-z0-9]+)\]>/ie", "$$1[$2]", $index_content);
The first one for replacing calls to variables, and the second one for one-dimensional arrays in format <=array[key]>.

Now, I could just add another line for two-dimensional arrays (since I'd almost never go further than two), but I'd like to have a single line of code which handles all of the variables, and arrays with as many dimensions as I like.

What I tried was this:

Code: Select all

$index_content = preg_replace("/<=([_a-z0-9]+)(\[[_a-z0-9]\]+)>/ie", "$$1$2", $index_content);
basically moving the square brackets to find the array keys into the parentheses for the second callback. The idea is that $2 in the replace would be something like "[key1][key2]".

After doing a bit more testing, the T_IS_SMALLER_OR_EQUAL error is just a later problem caused by the fact that the preg_replace isn't matching the code; it isn't related. The real problem is that the regexp just isn't correct.

That said, if it wouldn't be possible to do it easily, or less effective, I could just add that extra preg_replace for two-dim arrays, so it's not imperative that I get this sorted out.

Posted: Sat Apr 23, 2005 8:28 pm
by Bennettman
Someone shoot me... ¬_¬

The problem was that the key name search didn't have the + to make it recognise names longer than one letter.

Here's the fixed code for it:

Code: Select all

$index_content = preg_replace("/<=([_a-z0-9]+)(\[([_a-z0-9]+)\])*>/ie", "$$1$2", $index_content);
Works with variables or arrays of any depth. If anyone wants to use something like this, bear in mind it won't recurse, so if any of the replacement content contains any tags itself, they won't be replaced themselves unless you repeat the test (with do-while for example).