Re: preg_replace backreference value as a key to array



Dave wrote:
Hi,

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:
$arr([3]=>"aaa",[104]=>"bbb",[345]=>"ccc",[n]=>"etc");
$html='<html>....<a href="page.php?var=3">3</a><a
href="page.php?var=345">345</a> ...<a
href="page.php?var=n">n</a>...</html>';

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','\\1var=' .
eval('\$c=\'\3\'; return \$c;'),$html);
Outputs the matched var: i.e. 345, but:

$html=preg_replace('/(href="page\.php\?)(var=)([0-9]+)/si','\\1var=' .
eval('\$c=\'\3\'; 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','\\1var=' .
eval('\$c=\'\3\'; \$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! :)

Read the following:

http://fi.php.net/manual/en/function.parse-url.php
http://fi.php.net/manual/en/function.parse-str.php

.



Relevant Pages

  • preg_replace backreference value as a key to array
    ... I have been trying all sorts ... of functions to fix it and my last resort is to ask you guys and girls. ... Dave ...
    (comp.lang.php)
  • Re: How to fix "Late Show" Part One
    ... When attempting to fix something, the most important thing to do is to assess the situation and determine how bad the situation really is, whether a fix is a small tweak or a major overhaul, and how pressing the need for the fix is. ... Taking the show on the road is stressful for the host, staff, and crew, and probably more expensive with HD requirements. ... When Dave started hosting a talk show, the talk show template was formed by Johnny, Merv, and Mike Douglas. ...
    (alt.fan.letterman)
  • Re: Media Player series 9 keeps entering setup thingy
    ... I'd love to have you try something for me that should fix it on your system. ... You can reach me at zachd at microsoft dot com. ... This posting is provided "AS IS" with no warranties, ... "Dave" wrote in message ...
    (microsoft.public.windowsxp.video)
  • Re: Need help with .hlp
    ... Dave - do you have EasyCleaner installed on your computer? ... There was a *.reg file posted on their site to fix it. ... Rename it to .hlp and Close The Registry>>> 6. ... On clicking an .hlp file, the little timeglass appears for a few ...
    (microsoft.public.windowsxp.general)
  • Re: error mail is corrupted - pls help!
    ... My computer will freeze ... Do I want to switch to live e-mail? ... all down b4 I do it, how exactly do I fix it? ...
    (microsoft.public.windows.vista.mail)