Jump to content

special character from db, GOOD ... from php, BAD


Recommended Posts

hello;

 

I have:

mysql: utf8_general_ci

index.php:  header( ... UTF-8)

index.php: <meta ... content-type ... UTF-8>

index.php, mysql query procedure: ... mysql_set_charset( utf8 , .. ) ...

 

So, if I put a special character in my db, it WILL display correctly in index.php

But, if I put the same character in a php variable, it is BAD ... diamond-shape with question mark inside

 

Since my special characters work from the db, I would like to also use them in html (index.php ).

 

am I missing anything? thanks for your time .. Shannon

 

 

IT should work from the PHP variable as well, I am going to assume you believe it doesn't as you have typed it into the code and fails, this is because the PHP file itself is not UTF-8,

 

for example if you read the text from the database then it will go into a PHP variable, and then you should be able to echo it out (assuming you have a UTF-8 char-set).

 

I don't think your missing anything but then again I'm not sure of the question/problem!  :-\

weird weird weird ....

 

I did not mention in the original post that there are actually 2 php pages, one included by the other.

 

I determined that the variables from the included file were the problem (if I moved them to the main.php, no problems).

 

I use dreamweaver as my text editor AND APPARENTLY dreamweaver saves the files with an encoding, and indeed the included.php was saved with Western encoding.

 

in dreamweaver: CTRL-J ... TITLE/ENCODING ... ENCODING

 

so even though I had header( ... UTF-8) all over the place, the included.php did not care as it was saved in Western encoding.

I am unfamiliar with the idea that a .php (which is just plain text) can have itself be 'utf8' or whatever.

 

Where is this info stored? If I take my .php file, upload to a remote server ... where is that data from dreamweaver stored ... there's nothing visible in a text editor showing that  DW injected something into it.

 

thanks,

Shannon

Most editor don't display the encoding but it is their,

 

If your editing the file in dreamweaver, then your need to tell DW that the file needs to be UTF-8

Edit->preferences->New Document->encoding (set to UTF-8)

then create a new document and copy the contents from the old file to the new one, (if the text is look wrong your need to re-enter the text)

 

here is an example of 2 files.

http://madtechiesupport.com/phpfreaks/UTF8/ASCII.php

http://madtechiesupport.com/phpfreaks/UTF8/UTF-8.php

 

and the code

http://madtechiesupport.com/phpfreaks/UTF8/code.zip

 

Now your see that the UTF-8 file had this

$array = array(

'Hello',

'¡Hola',

'مرحبا',

'Γεια σας',

'您好',

);

While the ascii has

$array = array(

'Hello',

'¡Hola',

'?????',

'Ge?a sa?',

'??',

);

 

If you open the ascii.php and type any special chars they won't display because the file is ascii encoded, BUT they will work in the UTF-8 file.

 

So the question is does the text display correctly in the file (when in the editor)

 

do you have a sample we can see ?

 

I wish I was getting the same result as you ... within my texteditor (dreamweaver) characters display fine, regardless of whether I am using Western or utf8. But when I look at it in the browser, then I get the ?-marks when having used the Western.

.

<?php header('Content-Type:text/html; charset=UTF-8'); 

$gvs = "Č芚ŽžÄäÉéÖöÜüß"   ;   
print $gvs ;
?>

 

If I was getting the ?-marks in the texteditor environment, I would actually be relieved: wysiwyg, which is always reliable :) So I guess I am still confused about where the texteditor's encoding is actually stored, since it doesn't seem to be stored in the .php's visible space.

 

-Shannon

You say you get the ? when using Western, may i ask why your using Western instead of UTF-8 or are you getting the same for both ?

Can you upload a file that doesn't work, (just a sample) posting the code won't help as much as this site will convert them.

this is a php file ... it's only content is:

<?php header('Content-Type:text/html; charset=UTF-8'); 
$gvs = "Č芚ŽžÄäÉéÖöÜüß"   ;   
print $gvs ;
?>

 

my text editor is dreamweaver.

 

currently this file has been encoded as Western. But notice the header(... utf-8 ). low and behold the DW setting dominates even when the php file IS NOT in the text editor environment, but rather in a browser environment.

 

Ivan, MadTechie ... I wasn't really using Western ... in fact I was unaware that DW even had this setting available at all.

 

So my question is: where is the DW encoding setting actually being stored ... there's no visible indicators in the text editor.

 

if I change the DW setting to 'utf8', all is well in the browser.

 

- thanks, Shannon

17482_.php

I just checked the file you uploaded and it was encoded as ASCII,  this means the data is stored in a ASCII format, and your outputting as a UTF-8 format..

 

ASCII: http://madtechiesupport.com/phpfreaks/UTF8/test_special_chars_DW_encoding_setting.php

UTF-8: http://madtechiesupport.com/phpfreaks/UTF8/test_special_chars_DW_encoding_setting_v2.php

*UTF-8 without BOM: http://madtechiesupport.com/phpfreaks/UTF8/test_special_chars_DW_encoding_setting_v2.php

 

*Recommended

 

Note that I took off the header v2, as the BOM would mess it up, but in V3 its fine,

 

 

Download the V3 and open it in DM and replace its contents with your contents and it should be fine,

 

your need to tell DW that the file needs to be UTF-8

Edit->preferences->New Document->encoding (set to UTF-8)

then create a new document and copy the contents from the old file to the new one, (if the text is look wrong your need to re-enter the text)

17483_.php

17484_.php

Think of it this way!

 

BOM is a hidden text file header..

Now ignore that as i use UTF-8 without BOM

 

Now just say

65 = A in ASCII

and

0065 = A in UNICODE (UTF-8)

 

So if i view A in UNICODE (its raw data is 0065)

And if i view A in ASCII (its raw data is 65)

 

BUT

if I view 0065 in ASCII it would show [nul]A  (00 being nul)

 

How about other characters

for example ß in UNICODE is basically ß ASCII (but looks better in UNICODE)

 

this is because ASCII only has 255 characters, so displays the bytes as 2 character instead of 1.

 

for more details see here or google

 

 

in any case, PHP files are text but they can be encoded many different ways, the encoding is used to store and display the text,

for example

should it display 2 bytes as ß or ß

 

 

I hope that clears thing up

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.