0

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?

Tanja Pakovic
  • 193
  • 1
  • 3
  • 14
  • What is setting the `txtPretraga` value in your URL query string? I would recommend replacing the spaces with '-' before adding it to the query string. – Adam Plocher Mar 07 '13 at 22:22
  • Thats not a very clean URL anyway... (-: – jtheman Mar 07 '13 at 22:22
  • Dont worry, before it goes to query i done a double check,stip all, real escape etc, the problem is in my URL – Tanja Pakovic Mar 07 '13 at 22:23
  • Whatever is constructing that URL is not encoding it properly. Those should be `%20` instead of `%` if they're supposed to be representing spaces. Use [`urlencode()`](http://www.php.net/manual/en/function.urlencode.php) – Sammitch Mar 07 '13 at 22:32
  • The correct answer will be something in RewriteRule – Tanja Pakovic Mar 07 '13 at 22:34

2 Answers2

3

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.

ntgCleaner
  • 5,865
  • 9
  • 48
  • 86
  • No, no, not the problem in query the problem is in URL – Tanja Pakovic Mar 07 '13 at 22:24
  • Well, your problem is that you have spaces in the URL - so I would do a replace " " (space) with "" (no space) – ntgCleaner Mar 07 '13 at 22:25
  • Ok, but how to do that, that sting comes from get form? – Tanja Pakovic Mar 07 '13 at 22:26
  • During the post of your $_GET, you want to clean their string first - replace all spaces with dashes. If this is still not what you are looking for, I will need to see your code – ntgCleaner Mar 07 '13 at 22:26
  • Ok, i only knows when someone push submit button, it goes to another page, how to catch that before it goes to another page from main scope? – Tanja Pakovic Mar 07 '13 at 22:30
  • How do you pass the $_GET value? You just have to pass a clean get variable. You said above in your comments that you strip it, real escape, etc. Now, you need to take that 'clean' string and clean it again. Take that clean variable and use it with str_replace, then do the $_GET action with that – ntgCleaner Mar 07 '13 at 22:31
  • I pass it in get form to another page, the problem is not when it comes to page, i can to wih that what i want, but the problem is for me before GET goes to another page, i have to catch it :) Or maybe rewrite url? – Tanja Pakovic Mar 07 '13 at 22:37
  • Would you mind posting the code on how you pass your variable into your $_GET? Is it through a
    with a submit button?
    – ntgCleaner Mar 07 '13 at 22:39
  • Yes through a form, maybe this is a question for javascript? – Tanja Pakovic Mar 07 '13 at 22:40
  • Nope, everything should be fine with php - I'll update my answer with an example – ntgCleaner Mar 07 '13 at 22:42
  • Sorry, looking into it more, you might actually need javascript. I was still thinking $_POST where you have time to do that. I'll update the code anyways – ntgCleaner Mar 07 '13 at 22:49
  • Thanks mate i wrote it myself – Tanja Pakovic Mar 07 '13 at 23:01
0

Ok, i got the fix for the problem :)

This is not question for PHP, it is for Javascript and i wrote this function

<script type="text/javascript">
function provera() {
var x=document.forms["hsearch"]["txtPretraga"].value;
var n=str.replace(" ","-");
}
</script>
Tanja Pakovic
  • 193
  • 1
  • 3
  • 14