Reputation: 71
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 ženk!</tag>
Now this is the text I will be inserting into my database so I need ž
converted to character 'ž
' (its ASCII code).
Upvotes: 1
Views: 360
Reputation: 2515
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 ž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` ...");
Upvotes: 0
Reputation: 687
Use html_entity_decode()
and explicitly specify the charset:
$string = html_entity_decode($string, ENT_QUOTES, "utf-8");
for future reference: PHP string functions
Upvotes: 2
Reputation: 21
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
Upvotes: 0
Reputation: 394
Try below code.
$input = "Hello! My name is ž";
$output = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $input);
echo $output;
hope this helps.
Upvotes: 0