1

PHP: How to escape the URL params and path without encoding the Scheme and Host in URL:

Please notice* this url is just an example params might be named different than q and some urls might have path others might not, like the example below.

I get the url as follows:

http%3A%2F%2Fgoogle.com%2F?q=My%20Search%20Keyword 

Then I should converted to the following:

http://google.com/?q=My%20Search%20Keyword 

not:

http://google.com/?q=My Search Keyword 

is there an easy way for doing this, other than

  1. Url decoding the whole url, via "rawurldecode"
  2. "parse_url" to split it to scheme, domain, path, query
  3. Parse the query with parse_str and escape the values only.
  4. Rebuild the query string
  5. Rebuild the url back,
  6. All the steps above require checking for empty values too.

UPDATE

In more complicated case I get the URL like this:

http://www.someUrl.com/?redirect=http%3A%2F%2Fgoogle.com%2F%3Fq%3DMy%20Search%20Keyword%26hl%3Den 

Where I search for =http..... then I use this part to crawl it via Curl and get the http response code from this url, the problem is that I can't easily prepare the url which was sent ecnoded from the beginning without involving the complicated process I mentioned above.

is there an easier way of doing this ?

10
  • you could explode on ?q= if thats the only variable in the strong Commented Feb 4, 2014 at 19:07
  • This is just an example, I'm processing millions of different urls, none of them are Google urls Commented Feb 4, 2014 at 19:08
  • well i can only make segestions based on what you post Commented Feb 4, 2014 at 19:09
  • o.k. I added a note, that this is just an example url Commented Feb 4, 2014 at 19:10
  • strange idea, give an actual example Commented Feb 4, 2014 at 19:10

3 Answers 3

1

Can't you just explode on the ?, then rawurldecode the first part, then implode?

$url = "http%3A%2F%2Fgoogle.com%2F?q=My%20Search%20Keyword"; $parts = explode("?", $url); $parts[0] = rawurldecode($parts[0]); $url = implode("?", $parts); 
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I think this would work even if the Url is paramless.. Let me try first then I'll mark it correct or incorrect ..
I voted up for your Answer as it's not exactly the correct one, but it's an approach
where there are 2 params and both are escaped with the & sign escaped as well, may be it was a problem in my question not to mention all cases
1

Try this:

<?php $rawUrl = 'http%3A%2F%2Fgoogle.com%2F?q=My%20Search%20Keyword'; $arrDec = parse_url(urldecode($rawUrl)); $queryEnc = parse_url($rawUrl, PHP_URL_QUERY); $newUrl = $arrDec['scheme'] . '://' . $arrDec['host'] . '?' . $queryEnc; print_r($newUrl); ?> 

Here's a phpFiddle: http://phpfiddle.org/main/code/wmi-xet

7 Comments

how about when there are more than 1 params from the beginning? I'll update my question
Try my phpFiddle example. It works fine no matter how many params there are.
for more than 1 param, it doesn't work, please check the update I posted above
Your update is not clear. What you want to achieve with this last url?
crawl it with curl, to get the http response
|
0

I came up with the following solution, it might have flaws, but might help someone else as well ..

$url = rawurldecode('http%3A%2F%2Fgoogle.com%2F%3Fq%3DMy%20Search%20Keyword%26hl%3Den'); echo preg_replace_callback('#[^/\.\:&=\?]{1}#', function($str) { return rawurlencode($str[0]);}, $url ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.