Jump to content


Photo

Please help: Trouble with code, involves for(), preg_match, etc etc...


  • Please log in to reply
No replies to this topic

#1 Raxx

Raxx
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 26 July 2006 - 06:06 AM

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 :P

My 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?

<?php
# Setting up our mock variables
$context['foo'][0]['bar'] = "Foo Bar!";
$context['foo'][1]['bar2'] = "Foo Bar 2!";

# Making 10 mock ["options"] keys
for ($i = 0; $i < 10; $i++)
{
 		$context["options"][] = $i;
}

# Our function
function  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);
?>

If I swapped $o with any number from 0-1, it would replace the corresponding tag properly...for instance, :
$input_spliced = preg_replace("/<" . $tag_array[0] . ">/i", $replaced_array[0], $input1);

Would return:
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:

So I can't really understand why it won't work with $o...

It's supposed to return:
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!

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users