Search code examples
phpurlurl-rewritinghtmlspecialcharsmagic-quotes-gpc

Replace % with - in url PHP


Ok i have one maybe stupid question On my website i have search options, that is input with GET metod, but when someone enter long seperated word like

I AM SOMETIMES BLANK

i got this in my url

http://www.example.com/search.php?page=1&txtPretraga=I%AM%SOMETIMES%BLANK

I dont know how to change that? I want clean URL like this http://www.example.com/search.php?page=1&txtPretraga=I-AM-SOMETIMES-BLANK

I want to change % with - in my ULR

Any ideas?


Solution

  • You can use str_replace in your php code: http://php.net/manual/en/function.str-replace.php

    $search_qry = "Whatever%They%Type";
    $replace = str_replace($search_qry, "%", "-");
    

    EDIT: In the case of your strings - they have spaces, which show up as % in a URL, so use this before you do your $_GET

    $search_qry = "Whatever They Type";
    $replace = str_replace($search_qry, " ", "-");
    

    EDIT 2 Since this is a $_GET - Javascript will have to be used to clean the string before it's sent. (using jQuery and javascript here)

    <script type = "text/javascript">
        $(document).ready(function(){
            $('.example-input').blur(function(){
                var str = $(this).val();
                var clean_str = str.replace(" ", "-");
                $(this).val(clean_str);
            });
        });
    </script>
    

    This should clean the string in the input box before it's even sent through the get. or instead of .blur, you can use $('submit-button').click(function(){...

    Or, you can use the .htaccess file to do a mod rewrite. But I don't know that well enough.