Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Posts
    186
    Thanks
    72
    Thanked 2 Times in 2 Posts

    How to replace the value inside a URL with another one?

    Hello, I have a problem i'm stuck on. I am trying to write a function that will take the current URL and change the value of "page" to the value being passed into the method. So, for example, if the current URL is "http://mysite.com/?go=search&page=12&q=on", and the parameter being passed is 4, I would would want it to change it to "http://mysite.com/?go=search&page=4&q=on". What is the best way to do this?

    I guess it would be something like this:

    Code:
    function changePage($value)
    {
      $URL = getCurrURL(); // helper function
    
      // what do I do next? lol
    }
    Thanks in advance for your help!

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    89
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Hi,

    Please check http_build_url and similar functions: parse_url(), parse_str(), http_build_query().
    Regards
    URL2SEO URL shortener and redirection services

  • Users who have thanked koko5 for this post:

    skcin7 (03-05-2010)

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Yeah I would favour the functions above, although you could use a regular expression along with preg_replace to replace the value
    PHP Code:
    $URL preg_replace('/\bpage=\d+/''page='.$value$URL); 
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    skcin7 (03-05-2010)


  •  

    Posting Permissions

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