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 6 of 6
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts

    replace hash in URL

    I need to read the value of a PHP "get" value and i have no control over the content of this value (can't encode it before it reach the server).
    The problem is that when the value contains a # symbol, the server will not read anything past the # and consider it as an anchor value.

    Example:
    www.example.com/example.php?var=test#1

    $_SERVER['QUERY_STRING']; or $GET_['var'];
    should return
    test#1
    but it just returns
    test
    It happens every time a "#" symbol is inserted in the URL.

    After a couple of researches i found out that neither PHP or htaccess can do anything about it. The only solution would be with Javascript

    So far here's the closest thing i found:
    // remove fragment as much as it can go without adding an entry in browser history:
    window.location.replace("#");

    // slice off the remaining '#' in HTML5:
    if (typeof window.history.replaceState == 'function') {
    history.replaceState({}, '', window.location.href.slice(0, -1));
    }
    But it will just strip out the anchor tag and anything after it. I don't want to remove the "#" and the characters after it, i just want to replace the "#" by another symbol that will not break URL.

    Any idea ?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by anarchoi View Post

    But it will just strip out the anchor tag and anything after it. I don't want to remove the "#" and the characters after it, i just want to replace the "#" by another symbol that will not break URL.

    Any idea ?
    Why not simply use replace()?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Why not simply use replace()?
    Could you give me an example ? I'm really a newbie with javascript...

    i'd need something like that :

    if (window.location.contains = "#") {
    replace ("#" by "XYZ" in window.location.url);
    window.location.reload;
    }
    now i need help to translate that into real javascript..

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Here you are:-

    Code:
    <script type = "text/javascript">
    
    var r = window.location.href;
    if (r.match(/#/)) {
    r = r.replace(/#/gi, "XYZ");
    window.location.href = r;
    }
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    93
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks a lot !!! Spent a lot of time looking for a solution to this problem !

    Last thing:
    How can i also match %23 and replace it with XYZ ? Some browsers will automatically encode # to %23

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by anarchoi View Post
    Last thing:
    How can i also match %23 and replace it with XYZ ? Some browsers will automatically encode # to %23
    Code:
    if (r.match(/#|%23/)) {
    r = r.replace(/#|%23/gi, "XYZ");

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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