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ї'thing'] = "foobar";
$var = 'some text %thing_thing% some text';
$var = preg_replace("/(%)(.*)(_)(.*)(%)/", "echo $\\2ї'\\4'];", $var);
echo $var;
?>
now, that outputs
Code: Select all
some text echo $thingї'thing']; some text
i need it to output
Code: Select all
echo "some text " . $thingї'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ї'thing'] = "foobar";
$var = 'some text |%thing_thing%| some text';
$var = explode("|", $var);
foreach($var as $value){
if(!preg_match("/^(%)(.*)(%)$/", $value)){
$new .= $value;
} else {
$temp = preg_replace("/^(%)(.*)(_)(.*)(%)$/", "echo $\\2ї'\\4'];", $value);
$new .= eval($temp);
}
}
echo $new;
?>
now, that prints out
wtf? it went in the wrong order... and if add in a little $key=>$value and print out $key it prints out...
Posted: Sun Oct 06, 2002 2:39 pm
by hob_goblin
AHA!
I'VE DONE IT!
Code: Select all
<?
$thingї'thing'] = "foobar";
$var = 'some text |%thing_thing%| some text';
$var = explode("|", $var);
foreach($var as $key => $value){
if(!preg_match("/^(%)(.*)(%)$/", $value)){
$varї$key] = $value;
} else {
$temp = preg_replace("/^(%)(.*)(_)(.*)(%)$/", "return $\\2ї'\\4'];", $value);
$varї$key] = eval($temp);
}
}
echo "<pre>";
print_r($var);
echo "</pre>";
?>
prints out
Code: Select all
Array
(
ї0] => some text
ї1] => foobar
ї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.