Вопрос

Ive done a good deal of searching and so far can't find this already answered (though I might not be asking properly).

I have a simple title string on a page thats stored into a php variable

$title

When I print_r it I get back a happy, normal, string like I should

Art's of Glass: Photos From Crystal Castles' Gra

Now here is where Im lost. I run urlencode on this variable and I get back

urlencode($title);
Art%26%23039%3Bs+of+Glass%3A+Photos+From+Crystal+Castles%26%23039%3B+Gra

If you look at the first 10 chars, the urlencode did what it was supposed to, but it also did a little extra. It changed the "'" into it's html encoding ('), and THEN did urlencode on that resulting string.

When I print a manually encoded string

urlencode("Art's");
Art%27s

The result comes out correctly.

I can't for the life of me figure out why that extra step is happening in the first example. I've tried everything. I've htmlspecialchar_decode -d $title before encoding it, I've printed it every way possible, I've tried rawurlencode, I've tried every way of reversing the html special chars on the javascript side, but ended up nowhere. If anyone can shed some light, even some suggestions as to possible problems, that would be appreciated. I really want to avoid having to 'tailor' the solution to this particular instance as it might show up in other disguises later on.

Solution: Quentin and JMB helped sort this out, the original string was already HTML encoded, had to use htmlspecialchars_decode($title, ENT_QUOTES) to turn it into a normal string.

Это было полезно?

Решение

Your original data probably includes the HTML character reference for '

When you print_r it, your browser interprets it as HTML and renders a '.

Другие советы

I have found the solution for this, here it is;

<?php
$string = "amir'qureshi";
$postipn3 = urlencode(htmlspecialchars_decode($string, ENT_QUOTES));
//out put will be amir%27qureshi
?>

what i normally do is:

$string = htmlentities($string);
$string = html_entity_decode($string);

you can also do:

$string = htmlentities(base64_encode($string));
$string = html_entity_decode(base64_decode($string));

maybe thats overdoing it but i like it.

For what it's worth, when I run this code:

<?php
$title = "Art's of Glass: Photos From Crystal Castles' Gra";
$title = urlencode($title);
print_r($title);
?>

I get this result:

Art%27s+of+Glass%3A+Photos+From+Crystal+Castles%27+Gra

Can we see more of your code? Not sure why you're having that problem from just what you've provided...

Try just using the php snip I used and see if it does the same thing. If so, like someone else mentioned, it may be a goofy setting in your php configuration somewhere.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top