7

I have the following URL:
http://sub.mysite.com/store/?store=Fine+Ting&storeid=3726&test1=value1&test2=value2

Using print_r(parse_url($url)) gives me this:

Array ( 
  [scheme] => http 
  [host] => sub.mysite.com 
  [path] => /store/ 
  [query] => store=Fine+Ting& 
  [fragment] => 038;storeid=3726&test1=value1&test2=value2 
) 

According to the documentation I should only get fragment after the hashmark #.

Why then is parse_url returning fragment? Shouldn't that be in [query]?

Steven
  • 19,224
  • 47
  • 152
  • 257
  • 1
    I don't get a `fragment` element when I use your example: `var_dump(parse_url("http://sub.mysite.com/store/?store=Fine+Ting&storeid=3726&test1=value1&test2=value2"));`. Running php5.3.5 – Mike B Nov 02 '11 at 21:17
  • Works here too. Maybe you have an encoding issue. With the hashmark I see a `#038;` in there, that I cannot see anywhere in your url – KingCrunch Nov 02 '11 at 21:19
  • The function I use to return the URL uses Wordpress `esc_url`. This seems to use `htmlentities`. That's why I get the `038;` Thanks to @mAu for seeing this :) – Steven Nov 02 '11 at 21:25

1 Answers1

11

You have a problem with you url, the ampersand is encoded in htmlentities (&). Therefore parse_str thinks, the fragment is starting there.

Try using html_entity_decode before passing the url to parse_url.

mAu
  • 2,020
  • 1
  • 14
  • 27
  • Where do you get the url from? Are you doing any processing on it? – mAu Nov 02 '11 at 21:20
  • @mAu, thanks. Using `html_entity_decode` solved my issue. I'm using a custom function to return URL. This function uses Wordpress's `esc_url`. – Steven Nov 02 '11 at 21:23
  • Yep, that's producing the "error". As the docs state: "Encodes ampersands (&) and single quotes (') as numeric entity references (&, ')." ([esc_url docs](http://codex.wordpress.org/Function_Reference/esc_url)). – mAu Nov 02 '11 at 21:24