brynkaufman Posted November 13, 2007 Share Posted November 13, 2007 I have the following URL. test.php?test=%Beach% Then one line of code. echo ($_REQUEST['test']); and the result is. ¾ach% Why is the %B being read as 3/4 by the $_GET? Thanks. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 13, 2007 Share Posted November 13, 2007 % in a url followed by 2 hex chars is used for encoding characters. I think you'll find BE is ascii value (in hex) for 3/4 %20 gives a space character Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 13, 2007 Share Posted November 13, 2007 That's because the "%" character followed by another 2 characters is interpreted as an urlencoded character when it's on the URL and PHP is receiving the decoded value. You can reencode it: <?php $test = urlencode($_GET['test']); ?> Ken Quote Link to comment Share on other sites More sharing options...
Barand Posted November 13, 2007 Share Posted November 13, 2007 You need to encode it before putting it into the url. Test script: <?php if (isset($_GET['test'])) echo $_GET['test'], '<hr>'; ?> <a href='?test=%beach%'>TEST 1</a> <br> <?php $test = urlencode('%beach%'); echo "<a href='?test=$test'>TEST 2 </a>"; ?> Quote Link to comment Share on other sites More sharing options...
brynkaufman Posted November 13, 2007 Author Share Posted November 13, 2007 Sorry, I should have mentioned I tried to urlencode and this is what I get. echo urlencode($_GET['test']); gives %BEach%25 Notice the 25 after the last percent sign. I am trying to pass this search parementer - %Beach% - for MySQL through the URL so I can't have anything after the % or it won't find the right results. Also, the % is not intended to be a space, it is the wildcard for the search so anything with the word Beach in it will come back in the results. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 13, 2007 Share Posted November 13, 2007 Use a different character in the URL and replace it with a "%" in your script. Ken Quote Link to comment Share on other sites More sharing options...
brynkaufman Posted November 13, 2007 Author Share Posted November 13, 2007 Thanks Ken, I will just use * as my wildcard and change it to a % in the script. This seems to work fine. I thought there might be some function to get the % working but using a * for a wildcard will be even more familiar to my users. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.