How to properly URL encode a string in PHP?

0 votes
I'm making a search page, where you type a search query and the form is submitted to search.php?query=your query. What PHP function is the best and that I should use for encoding/decoding the search query?
Oct 18 in PHP by kartik
• 35,300 points
118 views

1 answer to this question.

0 votes

Hello @kartik,

For the URI query use urlencode/urldecode; for anything else use rawurlencode/rawurldecode.

The difference between urlencode and rawurlencode is that

  • urlencode encodes according to application/x-www-form-urlencoded (space is encoded with +) while
  • rawurlencode encodes according to the plain Percent-Encoding (space is encoded with %20).
Hope it helps!!
answered Oct 18 by Niroj
• 76,540 points

Related Questions In PHP

0 votes
1 answer

How to implement a callback in PHP?

Hello, Implementation of a callback is done like ...READ MORE

answered Apr 15 in PHP by Niroj
• 76,540 points
68 views
0 votes
1 answer

How can I connect to a Tor hidden service using CURL in PHP?

Hello @kartik, I use Privoxy and cURL to scrape Tor ...READ MORE

answered May 19 in PHP by Niroj
• 76,540 points
865 views
0 votes
1 answer

How to concatenate text from multiple rows into a single text string in SQL server?

Hello @kartik, Use COALESCE: DECLARE @Names VARCHAR(8000) SELECT @Names = ...READ MORE

answered Jul 21 in PHP by Niroj
• 76,540 points
480 views
0 votes
1 answer

How to get parameters from a URL string?

Hello @kartik, You can use the parse_url() and parse_str() for that. $parts = ...READ MORE

answered Aug 14 in PHP by Niroj
• 76,540 points
49 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
3,441 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
411 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
281 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
4,142 views
0 votes
1 answer

How to convert a string to JSON object in PHP?

Hello @kartik, it seems that your JSON is ...READ MORE

answered Oct 20 in PHP by Niroj
• 76,540 points
40 views
0 votes
1 answer

How to check if a string is an email address in PHP?

Hello, Try this without regular expressions: <?php ...READ MORE

answered Nov 3 in PHP by Niroj
• 76,540 points
96 views