preg_replace backreference value as a key to array

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

This is an interesting problem I'm faced with. I have been trying all sorts  
of functions to fix it and my last resort is to ask you guys and girls.

I have an array:
$html='<html>....<a href="page.php?var=3">3</a><a  
href="page.php?var=345">345</a> ...<a  

I'm trying to get a value from URLs in HTML and use it as a key to output  
the value of $arr.
i.e. matching the: /page.php?var=(345)/ will output: "ccc";

Here is what I've tried but can't quite get there:
$html=preg_replace('/(href="page\.php\?)(var=)([0-9]+)/si','var=' .  
eval('$c=''; return $c;'),$html);
Outputs the matched var: i.e. 345, but:

$html=preg_replace('/(href="page\.php\?)(var=)([0-9]+)/si','var=' .  
eval('$c=''; return $arr[$c];'),$html);
Won't output anything.

I tried to see if the backreferencfe value is really what it seems  
(integer), but when I do:
$html=preg_replace('/(href="page\.php\?)(var=)([0-9]+)/si','var=' .  
eval('$c=''; $c=gettype($c); return $c;'),$html);
it shows it's a string. If I try: $c*1, outputs is 0. If I try  
settype($c,"integer"), the output again is 0.

Another strange thing. This: "return $c;" returns the 345, but return  
strlen($c); returns 2.

Please, save me from this madness! :)



Re: preg_replace backreference value as a key to array

Dave wrote:
Quoted text here. Click to load it

Read the following:

Site Timeline