SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Replace Variables in URL

    Hello,

    I wondered if it was possible to rewrite a URL so designed for the rewrite wrote in htaccess

    e.g.
    I have the URL: $next_url = "$your_search_page?s=$news&kw=$append";

    The URL goes here: http://example.com/?s=20&kw=Garden/Center/Product

    But I want to change the URL to: http://example.com/Garden/Center/Product/P2.html

    This is just for the href, the htaccess bit is solved.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hmm, what about...
    PHP Code:
    <?php
    function funkify_url($url){
      list(
    $host$qs) = explode('?'$url);
      
    parse_str($qs$params);
      return 
    sprintf(
        
    'http://%s/%s/P%s.html',
        
    parse_url($hostPHP_URL_HOST),
        
    $params['kw'],
        
    $params['s']
      );
    }

    var_dump(
      
    funkify_url('http://www.example.org?s=20&kw=Garden/Center/Product')
    );

    /*
      string(53) "http://www.example.org/Garden/Center/Product/P20.html"
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi AnthonySterling,

    I have no idea what you have wrote here but I pasted this (below) on my page. When I click next (thats the link) it takes me to: http://example.com/?s=100&kw=Garden/Center/Product/P2

    I'm not sure if I'm adding this correctly. Also on my page are other '%s' symbols, I'm not sure if they interfer with each other. But I do notice ALL the links are going to: http://example.com/?s=100&kw=Garden/Center/Product/P2 ...except the "red" variables change.


    PHP Code:
    function funkify_url($url){
      list(
    $host$qs) = explode('?'$url);
      
    parse_str($qs$params);
      return 
    sprintf(
        
    'http://%s/%s/P%s.html',
        
    parse_url($hostPHP_URL_HOST),
        
    $params['kw'],
        
    $params['s']
      );

    Here is a look at a snipped of my code:

    PHP Code:
    if ($current_page != $pages) {
    $finals = ($pages 1) * $limit;
    $url "$your_search_page?s=$finals&amp;kw=$append";
                if (
    $sort_enabled == TRUE && isset($_GET['o'])) {
                
    $url .= "&amp;o=$o";
                }
    echo 
    "<a href=\"$url\">$last_text</a>";
    }
    }
    // check to see if last page
        
    if (!((($s+$limit)/$limit)==$pages)) {
    // not last page so give NEXT link 
    // new s value = s + number of rows per page

    function funkify_url($url){
      list(
    $host$qs) = explode('?'$url);
      
    parse_str($qs$params);
      return 
    sprintf(
        
    'http://%s/%s/P%s.html',
        
    parse_url($hostPHP_URL_HOST),
        
    $params['kw'],
        
    $params['s']
      );
    }


      
    $news=$s+$limit;
        
    $next_url "$your_search_page?s=$news&amp;kw=$append";
        if (
    $sort_enabled == TRUE && isset($_GET['o'])) {
        
    $next_url .= "&amp;o=$o";
        }
        echo 
    " | <a href=\"$next_url\">$next_text</a>\n";
      } 
    echo 
    "</div></center>";
    }

    Thanks

  4. #4
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been playing with your function, dose is matter about being a subdomain?

  5. #5
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Anthony,

    I'm still working on these paging URLs! I finally figured out how to connect the function to the variable. That was a big help I like this little function. The only trouble I'm having is the way I have it set up (below) it creates a URL like this:
    http://example.com/-P10.html

    It dosen't add the $news variable to the URL. I've worked with it very much and can't see anything wrong. Does it appear to you that I have it set up correct? I changed the slashes to dashes, they look better to me.

    Thanks

    PHP Code:
    function funkify_url($url){
      list(
    $host$qs) = explode('?'$url);
      
    parse_str($qs$params);
      return 
    sprintf('http://%s/%s-P%s.html'parse_url($hostPHP_URL_HOST), $params['kw'], $params['s']);
    }


          if (!(((
    $s+$limit)/$limit)==$pages)) {
      
    $news=$s+$limit;
        
        
    $next_url funkify_url("$your_search_page?s=$news&amp;kw=$append");
        if (
    $sort_enabled == TRUE && isset($_GET['o'])) {
        
    $next_url .= "&amp;o=$o";
        }
        echo 
    " | <a href=\"$next_url\">$next_text</a>\n"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •