PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Sun Oct 06, 2002 2:03 pm
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?
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Sun Oct 06, 2002 2:19 pm
Try breaking the string into an array and then echoing each element of the separately.
Mac
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Sun Oct 06, 2002 2:36 pm
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...
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Sun Oct 06, 2002 2:39 pm
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
)
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Sun Oct 06, 2002 2:42 pm
Cool.
Mac
hob_goblin
Forum Regular
Posts: 978 Joined: Sun Apr 28, 2002 9:53 pm
Contact:
Post
by hob_goblin » Sun Oct 06, 2002 3:17 pm
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.