RegExp is killing my mind

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

Post Reply
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

RegExp is killing my mind

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try breaking the string into an array and then echoing each element of the separately.

Mac
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Cool.

Mac
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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.
Post Reply