How to submit a GET value to Php that has & characters in it

Hello All,

How can we submit a GET Value to Php that has special characters such as & in it?
So as you know a GET uses the character & and ? to separate each item of the GET value.
How can we then submit a GET value that has these special characters in it while having multiple values with the values of course separated with ? and & ?

FYI, we offer a non-profit search engine and the queries with these special characters are breaking apart when ? and & are included in them, such as:
b&b london uk

Thanks
Dean

You could try replace the eventual special GET characters with their UTF numbers in the URL, like:

When included in serch terms:
%26 for &
%3F for ?
%3D for =

Thanks for that intel.
So if you have a search string such as:

b & b London that is near Kingscross for ? calls me not jim & bob

how would you replace the ? and & with replacement chars?

Thanks

You should probably use urlencode or rawurlencode

2 Likes

Yeah, What @Gandalf said.

Example:

echo urlencode("https://geeksforgeeks.org?b&b london uk");

Gandalf, Thanks. That solved the problem :slight_smile:
You know what it is interesting is that after you URLencode a string in the URL, when Php reads it via GET it reads it without the encoding! So you have to encode it on each append into GET

Technically speaking PHP decodes the GET parameters, making it possible to pass “reserved” characters (like & and #) through GET parameters in the first place :slight_smile:

If you are building the query string part of URLs, use http_build_query(). it automatically urlencodes the values.

Actually I have a follow up related question:

how do we encode a URL in JavaScript for it to pass it to Php via AJAX for processing?

So we have lets say following URL on client side, that needs to be sent via AJAX to Php:

analyzer.php?new_client=195470&desc_brief=The Best Air b & b in Dublin&
kw_rx=Airbnb, Airb&b Data, Air b & b Analytics, Dublin Listings, Airbnb Occupancy Rates, B & B rooms Dublin

How do we encode this in JS so that it can be passed via AJAX to Php and then how do we decode it on Php side?

Thanks

In Javascript, this is called encodeURIComponent(string) (or if using a full URL, encodeURI(string))

1 Like

Thanks. That did the Job nicely :slight_smile:
Now People can search with ? & etc. in their searches & can submit Sites for Indexing with same characters in them etc. etc. :slight_smile:
And it took only 1 day to update this. But when you are a tiny search engine, doing 100 peoples Job, you really need to balance so many balls in the air. Anyway,Thank U all

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.