是否有PHP函数将数据库中突出显示的字母转换为html代码?

是否有PHP函数将数据库中突出显示的字母转换为html代码?

问题描述:

There is a MySQL database containing data with accentuated letters like é. I want to display it in my PHP page , but the problem is that there are unrecognized characters displayed because of the accent. So is there a function to convert accent to HTML code , for example é is converted to é !

有一个 MySQL code>数据库,其中包含强调 code>字母的数据 比如é code>。 我想在我的 PHP code>页面中显示它,但问题是由于 accent code>而显示无法识别的字符。 那么有一个 function code>来将重音转换为HTML代码,例如é code>转换为& eacute; code>! p> DIV>

Rather than using htmlentities you should use the unicode charset in your files, e.g.

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

To be on the safe side, you can add the following meta tag to your html files:

<html>
  <head>
    <meta charset="utf-8" />

Then, make sure that your data base connection uses utf8:

mysql_connect(...);
mysql_select_database(...);

mysql_set_charset('utf-8');

Then, all browsers should display the special characters correctly.

The advantage is that you can easily use unicode characters everywhere in your php files - for example the copyright sign (©) or a dash (–) - given that your php files are encoded in utf-8, too.

you can easily make one yourself with str_replace:

function txtFormat($input){

    $output = str_replace('/\à/','&#201;',$input);
    $output = str_replace('/\è/','&#232;',$output);
    $output = str_replace('/\é/','&#233;',$output);
    $output = str_replace('/\ì/','&#236;',$output);
    $output = str_replace('/\ò/','&#242;',$output);
    $output = str_replace('/\ù/','&#249;',$output);

    return $output;

}

Use the following code

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

And don't encode your data via utf8_encode() function before inserting into database hope this will solve your problem :)