Raxx Posted July 26, 2006 Share Posted July 26, 2006 Ok, this might be a rather hard to understand piece of code, since it originally relied on elements outside of the function...so I placed some mock elements in there, so it should work if you run the php file. Please try to concern yourself with why it doesn't work properly rather than the aesthetics :PMy problem is, is that I'm trying to swap <tag> (no, <tag> is not xml, it's just an indicator for data replacement...it could have been {{tag}} just as easily) with a string, but looped however many times I need it to be looped. Below is the sample code...can anyone figure out what's wrong?[code]<?php# Setting up our mock variables$context['foo'][0]['bar'] = "Foo Bar!";$context['foo'][1]['bar2'] = "Foo Bar 2!";# Making 10 mock ["options"] keysfor ($i = 0; $i < 10; $i++){ $context["options"][] = $i;}# Our functionfunction tagReplaceLooped($tag_array, $replace_array){ #Globalize some variables global $input1, $context; $replaced_array = array(); # Here, we're wanting to do stuff for every time there's an ["options"] key for ($i = 0; $i < (count($context["options"])); $i++) { for ($p = 0; $p < (count($replace_array)); $p++) { # Yeah...don't ask why I have to use eval, if you don't understand then you don't need to know :P eval("\$replaced_array[] = " . $replace_array[$p] . ";"); } for ($o = 0; $o < (count($replaced_array)); $o++) { # For every <$tag_array[$o]>, we're replacing it with $replaced_array[$o]... # Well, we're trying to anyways, this is where I get stuck $input_spliced = preg_replace("/<" . $tag_array[$o] . ">/i", $replaced_array[$o], $input1); } $input_diced .= $input_spliced . "<p>"; } $input_final = $input_diced; return $input_final;}$input1 = "Foo Bar: <foobar>, Foo Bar 2: <foobar2>";$tagin = array("foobar", "foobar2");$tagout = array("\$context['foo'][\$i]['bar']", "\$context['foo'][\$i]['bar2']");echo tagReplaceLooped($tagin, $tagout);?>[/code]If I swapped $o with any number from 0-1, it would replace the corresponding tag properly...for instance, :[code]$input_spliced = preg_replace("/<" . $tag_array[0] . ">/i", $replaced_array[0], $input1);[/code]Would return:[code]Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar: Foo Bar!, Foo Bar 2:[/code]So I can't really understand why it won't work with $o...It's supposed to return:[code]Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar 2!Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar 2!Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar 2!Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar 2!Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar 2!Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar 2!Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar 2!Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar 2!Foo Bar: Foo Bar!, Foo Bar 2: Foo Bar 2!Foo Bar: Foo Bar!, Foo Bar 2:Foo Bar 2![/code]I'm not much of a programmer, and so I've been stuck on this problem for too many hours, if you know of even a hint as to why it's not working (or better yet, just plain know...), please tell me!Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.