Michael Ridley
Michael Ridley

Reputation: 10498

Why is rawurlencode() in PHP adding additional escape characters to ampersands?

I think I'm missing something obvious here but it is driving me crazy and I can't figure it out. I'm developing a WordPress plugin and part of it needs to take the WordPress post title and send that to a RESTful web service to do something else. So of course I want to rawurlencode() the post title since who knows what text might be in there. However, for some reason the output I'm getting has extra escape characters and I have no idea where they are coming from (and it's causing problems with the web service I'm calling obviously).

My code is fairly straight forward:

$topic = get_the_title($post_id);
$curl_post_fields = 'name=' . rawurlencode( $topic );  

Yet when I print the output of those two strings I get:

topic=a & b
name=a%20%26%23038%3B%20b

Whereas I would expect the URL encoded string to be

name=a%20%26%20b

I have no idea where that extra %23038%3B could be coming from. If I'm reading the encoding on that correctly it translates to #038; but I still don't know where it's coming from.

Upvotes: 2

Views: 1710

Answers (2)

markijbema
markijbema

Reputation: 4055

There seems to be a html encoding in between as well, instead of &, & is in the encoded string. Probably because & has to be escaped in html, and the get_title function escapes this using html_special_chars or something like that.

Upvotes: 3

Joseadrian
Joseadrian

Reputation: 4374

I had some problems with that when i used an older php version

Upvotes: 0

Related Questions