简体   繁体   中英

Convert HTML character reference to UTF-8

I am new to PHP and character conversions so the title of my question might be missleading.

I am parsing one website and in one string, I want to parse, is a special character like this:

<tag>Hello! My name is &#382;enk!</tag>

Now this is the text I will be inserting into my database so I need &#382; converted to character ' ž ' (its ASCII code).

Use html_entity_decode() and explicitly specify the charset:

$string = html_entity_decode($string, ENT_QUOTES, "utf-8");

for future reference: PHP string functions

Try below code.

$input = "Hello! My name is &#382";

$output = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $input);

echo $output;

hope this helps.

Use the below code to solve this issue

$string_to_convert="your string";

$utf8_converted_string=utf8_encode($string_to_convert);

echo $utf8_converted_string // Output the utf8 characters

The problem isn't in displaying the data in the browser, because I tried the following and it worked perfectly:

<?php echo '<tag>Hello! My name is &#382;enk!</tag>'; ?>

The problem isn't in saving the data in the database.

The problem is in retrieving this character from the database.

So you need to set the format to UTF-8 before querying the database:

$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");

If you're using mysqli:

$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);
if(mysqli_connect_errno()){
    printf("DB Connect failed: %s\n", mysqli_connect_error());
    exit();
}
// Add the UTF-8 Support
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");

// Query the database
$mysqli->query("SELECT column FROM `table` ...");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM