replace part of an url with a regular expression- having trouble

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpluttme
    New Member
    • Oct 2006
    • 4

    replace part of an url with a regular expression- having trouble

    I need to replace not just the ASC or DESC but the value it's sorting by. i.e. in a string like this:

    http://www.mysite.com/page1.php?valu...sort=ORDER+BY+uservalue1+DESC

    I've not had that much experience with regular expresions and haven't had any luck with figuring out a way to replace just the bold part in the string above (after ORDER+BY.) Bear in mind, uservalue1 and DESC after ORDER+BY may be something else, like uservalue3 ASC or uservalue 5 DESC.

    I've found some great resources including the comments on the php.net function page and this board, but am still stumped.

    Can anyone show me what would work and more importantly how the expression breaks down to find the string? Or, is there another way to do this that I've overlooked? Thanks!
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Show the code that builds the url link+parms, then we can start with building up the parameters.

    And do not forget to include the code within the appropriate [php] or [code] tags (see Posting Guidelines at the top of the forum).

    Ronald :cool:

    Comment

    • jpluttme
      New Member
      • Oct 2006
      • 4

      #3
      I'm grabbing the url from the browser using the code below:

      [PHP]
      $host = $_SERVER['HTTP_HOST'];
      $self = $_SERVER['PHP_SELF'];
      $query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
      $url = !empty($query) ? "http://$host$self?$que ry" : "http://$host$self";

      echo $url;[/PHP]

      It is the $url value that contains the text I wish to replace.

      Thank you for your help.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        In the following snippet I urldecoded the url string. Then I used stripos to find the ORDER BY string. Copied the url (incl. ORDER BY) and appended whatever you want to append. Then reconstructed the string urlencoding ORDER BY plus the new parameter. See if it is useable in your case. (the echoes are just for testing).
        [php]<?php
        $url = "http://www.mysite.com/page1.php?val.. .&sort=ORDER+BY +uservalue1+DES C";
        echo "1. $url<br>";
        $url = urldecode($url) ;
        echo "2. $url<br>";
        $pos=stripos($u rl, "order by");
        if (!$pos)
        echo "String not found";
        else {
        $len = $pos+1+8; // length of ORDER BY literal + 1
        $pos++;
        $url1 = substr($url, 0, $len) . "new sort attributes";
        $url = substr($url1, 0, $pos) . urlencode(subst r($url1, $pos));
        echo "3. $url<br>";
        }
        if (!function_exis ts("stripos")) { // if using PHP4
        function stripos($str,$n eedle) {
        return strpos(strtolow er($str),strtol ower($needle));
        }
        }
        ?>
        [/php]

        Ronald :cool:

        Comment

        • jpluttme
          New Member
          • Oct 2006
          • 4

          #5
          THANK YOU!!! I'll play with it and see how it works.

          Originally posted by ronverdonk
          In the following snippet I urldecoded the url string. Then I used stripos to find the ORDER BY string. Copied the url (incl. ORDER BY) and appended whatever you want to append. Then reconstructed the string urlencoding ORDER BY plus the new parameter. See if it is useable in your case. (the echoes are just for testing).
          [php]<?php
          $url = "http://www.mysite.com/page1.php?val.. .&sort=ORDER+BY +uservalue1+DES C";
          echo "1. $url<br>";
          $url = urldecode($url) ;
          echo "2. $url<br>";
          $pos=stripos($u rl, "order by");
          if (!$pos)
          echo "String not found";
          else {
          $len = $pos+1+8; // length of ORDER BY literal + 1
          $pos++;
          $url1 = substr($url, 0, $len) . "new sort attributes";
          $url = substr($url1, 0, $pos) . urlencode(subst r($url1, $pos));
          echo "3. $url<br>";
          }
          if (!function_exis ts("stripos")) { // if using PHP4
          function stripos($str,$n eedle) {
          return strpos(strtolow er($str),strtol ower($needle));
          }
          }
          ?>
          [/php]

          Ronald :cool:

          Comment

          Working...