having trouble with string replacement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    having trouble with string replacement

    Hello,

    I have strings on my page of the form

    param1=abcd&age ncy_id=1234&par am2=dex

    I want to write a PHP expression to take strings similar to the above,
    and output

    param1=abcd&age ncy_id=<?php echo $LTON; ?>&param2=dex

    How can I do this?

    Thanks, - Dave

  • maarten.laurs@astrazeneca.com

    #2
    Re: having trouble with string replacement

    You just did!

    I don't get it. What's the problem?

    Comment

    • Erwin Moller

      #3
      Re: having trouble with string replacement

      laredotornado@z ipmail.com wrote:
      [color=blue]
      > Hello,
      >
      > I have strings on my page of the form
      >
      > param1=abcd&age ncy_id=1234&par am2=dex
      >
      > I want to write a PHP expression to take strings similar to the above,
      > and output
      >
      > param1=abcd&age ncy_id=<?php echo $LTON; ?>&param2=dex
      >
      > How can I do this?
      >
      > Thanks, - Dave[/color]

      Hi Dave,

      Looks fine to me.
      What is your problem?

      What does it produce that you didn't expect?
      Does $LTON have a value?

      Regards,
      Erwin Moller

      Comment

      • laredotornado@zipmail.com

        #4
        Re: having trouble with string replacement

        I actually want the result string (what is printed to the browser) to
        look like

        param1=abcd&age ncy_id=<?php echo $LTON; ?>&param2=dex

        This PHP code:

        <?php
        $str = "&mine=20&agenc y_id=234&test=a bc";
        $search = "/agency_id=\\d+/i";
        $replace = "agency_id=<?ph p echo \$LTON; ?>";
        $retVal = preg_replace($s earch, $replace, $str);
        print $retVal;
        ?>

        regrettably produces the wrong result

        &mine=20&agency _id=&test=abc

        How can I heal the pain? Thanks, - Dave

        Comment

        • Erwin Moller

          #5
          Re: having trouble with string replacement

          laredotornado@z ipmail.com wrote:
          [color=blue]
          > I actually want the result string (what is printed to the browser) to
          > look like
          >
          > param1=abcd&age ncy_id=<?php echo $LTON; ?>&param2=dex
          >
          > This PHP code:
          >
          > <?php
          > $str = "&mine=20&agenc y_id=234&test=a bc";
          > $search = "/agency_id=\\d+/i";
          > $replace = "agency_id=<?ph p echo \$LTON; ?>";
          > $retVal = preg_replace($s earch, $replace, $str);
          > print $retVal;
          > ?>
          >
          > regrettably produces the wrong result
          >
          > &mine=20&agency _id=&test=abc
          >
          > How can I heal the pain? Thanks, - Dave[/color]

          Hi Dave,

          The answer is actually very simple:
          Your desired line:
          param1=abcd&age ncy_id=<?php echo $LTON; ?>&param2=dex

          is not a legal url.

          You should urlencode all paramvalues before using them in an URL.

          So try something like:
          $theURL = "?param1=".urle ncode("abcd");
          // not very usefull, but you should do it with values
          // that possible contain difficult character.

          $theURL .= "&agency_id=".u rlencode("<?php echo $LTON; ?>");

          $theURL .= "&param2=".urle ncode("abc");


          That should work.
          Check your results by starting your script with:

          <pre>
          <? print_r($_GET); ?>
          </pre>


          It will spit out all name/value-pairs.

          Good luck,
          Erwin Moller

          Comment

          • laredotornado@zipmail.com

            #6
            Re: having trouble with string replacement

            Thanks for your reply. However, know that what is happening is that
            the result of the search and replace will be output to another PHP
            file, which will then be interpreted. So

            param1=abcd&age ncy_id=<?php echo $LTON; ?>&param2=dex

            is the output that I want to go to the PHP file. That file will then
            be interpreted, and the legal URL will be formed from that.

            Sorry I didn't mention that earlier. Thanks for any further
            assistance, - Dave

            Comment

            • frizzle

              #7
              Re: having trouble with string replacement

              Just try to put what you want directly into the address bar,
              and you'll see (most ?) browsers will translate it to an url-safe
              address, which means
              <?php echo $LTON; ?>
              becomes
              %3C?php%20echo% 20$LTON;%20?%3E

              Greetings Frizzle.

              Comment

              • feed_sheep

                #8
                Re: having trouble with string replacement

                <laredotornado@ zipmail.com> wrote in message
                news:1131133990 .080538.174740@ f14g2000cwb.goo glegroups.com.. .
                [color=blue]
                > <?php
                > $str = "&mine=20&agenc y_id=234&test=a bc";
                > $search = "/agency_id=\\d+/i";
                > $replace = "agency_id=<?ph p echo \$LTON; ?>";
                > $retVal = preg_replace($s earch, $replace, $str);
                > print $retVal;
                > ?>[/color]

                I can't see how you have it set up, but if a previous page does not pass the
                variable $LTON, it will of course equal nothing. Also, you are putting
                <?php ?> instead of <?php ?> tags.

                This code works:

                <?php
                $LTON = 1;
                $str = "&mine=20&agenc y_id=234&test=a bc";
                $search = "/agency_id=\d+/i";
                $replace = "agency_id= " . $LTON;
                $retVal = preg_replace($s earch, $replace, $str);
                print $retVal;
                ?>

                David


                Comment

                • feed_sheep

                  #9
                  Re: having trouble with string replacement

                  > I can't see how you have it set up, but if a previous page does not pass[color=blue]
                  > the variable $LTON, it will of course equal nothing. Also, you are
                  > putting <?php ?> instead of <?php ?> tags.[/color]

                  ^^^ I meant "inside".


                  Comment

                  Working...