English 中文(简体)
URL Decoding in PHP
原标题:

I am trying to decode this URL string using PHP s urldecode function:

urldecode("Ant%C3%B4nio+Carlos+Jobim");

This is supposed to output...

 Antônio Carlos Jobim 

...but instead is ouptutting this

 Antônio Carlos Jobim 

I ve tested the string in a JS-based online decoder with great success, but can t seem to do this operation server side. Any ideas?

最佳回答

Your string is also UTF-8 encoded. This will work:

echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim"));

Output: "Antônio Carlos Jobim".

问题回答

Actually, you get the desired output, but it is not interpreted as UTF-8. If this is on an HTTP application, you should send a header or a meta tag (or both) which would tell the client to use UTF-8.

Edit: for example:

// replace text/html with the content type you re using
header( Content-Type: text/html; charset=UTF-8 );

when I do

<?php
echo urldecode("Ant%C3%B4nio+Carlos+Jobim");
?>

Its display correctly in my browser like

Antônio Carlos Jobim

I have tested with XAMPP

another option is:

<?php
$smthing =  http%3A%2F%2Fmysite.com ;
$smthing = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\1;",urldecode($smthing)); 
$smthing = html_entity_decode($smthing,null, UTF-8 );
echo $smthing;
?>

output becomes : http://mysite.com

Are you also using htmlenteties before echoing it to the page? When I just tested your code it worked fine with just the urldecode("Ant%C3%B4nio+Carlos+Jobim"); part, but when I ran it through htmlentities I got the same output as you did.

It seems to be a problem with the UTF-8 characters and how PHP handles the htmlentities function.

first you have to decode in urldecoder function "urldecoder()" and then use utf decoder function "utf_decoder()"





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签