Page 1 of 1

RegExp is killing my mind

Posted: Sun Oct 06, 2002 2:03 pm
by hob_goblin
Im trying to replace %thing_thing% with the value of $thing['thing']

Here is what i have so far:

Code: Select all

<?
$thing&#1111;'thing'] = "foobar";
$var = 'some text %thing_thing% some text';
$var = preg_replace("/(%)(.*)(_)(.*)(%)/", "echo $\\2&#1111;'\\4'];", $var);
echo $var;
?>
now, that outputs

Code: Select all

some text echo $thing&#1111;'thing']; some text
i need it to output

Code: Select all

echo "some text " . $thing&#1111;'thing'] . " some text";
any ideas?

Posted: Sun Oct 06, 2002 2:19 pm
by twigletmac
Try breaking the string into an array and then echoing each element of the separately.

Mac

Posted: Sun Oct 06, 2002 2:36 pm
by hob_goblin
okay, so i tried this:

Code: Select all

<?
$thing&#1111;'thing'] = "foobar";
$var = 'some text |%thing_thing%| some text';
$var = explode("|", $var);
	foreach($var as $value)&#123;
	if(!preg_match("/^(%)(.*)(%)$/", $value))&#123;
	$new .= $value;
	&#125; else &#123;
	$temp = preg_replace("/^(%)(.*)(_)(.*)(%)$/", "echo $\\2&#1111;'\\4'];", $value);
	$new .= eval($temp);
	&#125;
	&#125;
echo $new;
?>
now, that prints out

Code: Select all

foobarsome text some text
wtf? it went in the wrong order... and if add in a little $key=>$value and print out $key it prints out...

Code: Select all

01foobarsome text 2some text

Posted: Sun Oct 06, 2002 2:39 pm
by hob_goblin
AHA!

I'VE DONE IT!

Code: Select all

<?
$thing&#1111;'thing'] = "foobar";
$var = 'some text |%thing_thing%| some text';
$var = explode("|", $var);
	foreach($var as $key => $value)&#123;
	if(!preg_match("/^(%)(.*)(%)$/", $value))&#123;
	$var&#1111;$key] = $value;
	&#125; else &#123;
	$temp = preg_replace("/^(%)(.*)(_)(.*)(%)$/", "return $\\2&#1111;'\\4'];", $value);
	$var&#1111;$key] = eval($temp);
	&#125;
	&#125;
echo "<pre>";
print_r($var);
echo "</pre>";
?>
prints out

Code: Select all

Array
(
    &#1111;0] => some text 
    &#1111;1] => foobar
    &#1111;2] =>  some text
)

Posted: Sun Oct 06, 2002 2:42 pm
by twigletmac
Cool.

Mac

Posted: Sun Oct 06, 2002 3:17 pm
by hob_goblin
Well, I needed it to access XML attributes, I'll show everybody later when I am finished... but it really had alot to do with this:

viewtopic.php?t=3395

I didn't really want to have to resort to regular expressions, but oh well, you do what you have to.