开发者

Encode ● special character in php

开发者 https://www.devze.com 2023-03-23 16:06 出处:网络
I need to store the special char ● inside a database. I\'m doing this: htmlentities($text,ENT_QUOTES,\'UTF-8\')

I need to store the special char ● inside a database.

I'm doing this:

htmlentities($text,ENT_QUOTES,'UTF-8')

for reasons I don't understand the char ● does not get encoded, it remains in its "normal" (●开发者_C百科) form instead of its encoded (●) form, it looks like this character is part of the utf-8 charset, but I need to have it encoded (●) anyway in the db. I cannot use another charset because I need UTF-8.

Why does this happen?

Thanks

EDIT: It's not a discussion about encoding or not encoding chars in the database, I would like to know why that particular char gets completely ignored.


You can use the multi-byte functions to convert your UTF-8 string to US-ASCI while replacing any non-ASCII character by a character reference:

mb_substitute_character('entity');
$ascii = mb_convert_encoding($utf8, 'ASCII', 'UTF-8');

Although I don’t see any reason to do this either. Your database won’t interpret any string as HTML.


The built-in function with a widest set of HTML entities is probably mb_convert_encoding:

<?php
// Assuming UTF-8 input:
echo mb_convert_encoding('●', 'HTML-ENTITIES', 'UTF-8');

In any case, once you've stored plain text as HTML, it's difficult to get it back to plain text (or re-encode it if specs change). I'd recommend storing stuff as-is.

0

精彩评论

暂无评论...
验证码 换一张
取 消