Home > Mobile >  Why is my PHP variable displayed as incomprehensible with a character sequence of % instead of its r
Why is my PHP variable displayed as incomprehensible with a character sequence of % instead of its r

Time:06-07

I'm trying to use this embed PHP Package (https://github.com/oscarotero/Embed/). However, I when I try to display the display of the Title with the variable $info->title of all the Pages having the Asian languages like that of the URL https://zh.wikipedia.org/wiki/亞馬遜公司, this returns me 亞馬遜公司 as title instead of this as the real title 亞馬遜公司.

Why the display of an incomprehensible character like: 亞馬遜公司 instead of 亞馬遜公司? ??

So how do I get my $info->title variable to display as it should: 亞馬遜公司 ???

Thank you please help me.

CodePudding user response:

You can decode the string. Maybe the library does not.

$title = '亞馬遜公司';
var_dump(rawurldecode($title));

will print

string(15) "亞馬遜公司"

CodePudding user response:

You should include a header like this before any output in your file: <?php header('Content-type: text/html; charset=UTF-8'); ?>.

Additionally, if you are getting your data from a database, you will need to make sure it is encoded and retrieved from the database as UTF8.

If you are outputting HTML, you will want to include the following element nested in your <head>: <meta charset="utf-8" />. You may also want to declare the language using the lang attribute on the <html> element.

CodePudding user response:

The zh.wikipedia URL you showed us is URL-encoded. To display it on a page you'll have to URL-decode it then HTML-encode it before you echo it or otherwise send it to a browser.

$displayableTitle = htmlentities( 
       urldecode( '亞馬遜公司' ) );
  • Related