preg_match() question: Operation to occurrence does not result

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thenetflyer

    preg_match() question: Operation to occurrence does not result

    <?php

    /*
    Using a function to perform an operation to an occurrence before
    returning it to the replacement string does not work in preg_replace.
    Consider the following code in which I want to urlencode the
    replacement string which does not work.
    I tried to call another function but that did not work either.

    May be I do something wrong ???

    Thanks,

    Klaas


    */

    $string = "<a href=\"/path/path2/script.php?sess ion_id=zEFCFClD mmlFFHE15416&op tion=myoption\" >My
    Option</a><br>";

    echo "urlencode the parameter part: [" .
    preg_replace('/(<a.+href=\")([^\"]+)/i', "$1". urlencode('\\2' ),
    $string) . "]\n";
    echo "uppercase the parameter part: [" .
    preg_replace('/(<a.+href=\")([^\"]+)/i', "$1". strtoupper("$2" ),
    $string) . "]\n";

    /*

    Content-type: text/html
    X-Powered-By: PHP/4.3.3RC4

    urlencode the parameter part: [<a href="%5C2">My Option</a><br>]
    uppercase the parameter part: [<a
    href="/path/path2/script.php?sess ion_id=zEFCFClD mmlFFHE15416&op tion=myoption"> My
    Option</a><br>]
    */

    ?>
  • Pedro Graca

    #2
    Re: preg_match() question: Operation to occurrence does not result

    thenetflyer wrote:
    ....[color=blue]
    > $string = "<a href=\"/path/path2/script.php?sess ion_id=zEFCFClD mmlFFHE15416&op tion=myoption\" >My
    > Option</a><br>";
    >
    > echo "urlencode the parameter part: [" .
    > preg_replace('/(<a.+href=\")([^\"]+)/i', "$1". urlencode('\\2' ),[/color]
    (snip)

    You need to specify the /e option in the regular expression


    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    Working...