I need to get only 30 characters from the paragraph submitted by user. In case the 30th character is an emoji, the output shows question marks. How can I avoid breaking the emojis?
echo substr("Hello world Hello world Hellð ", 0, 30);
Output: Hello world Hello world Hell��
Also, when using json_encode to return the output, the output is blank.
$myvariable = array();
$myvariable['hello'] = substr("Hello world Hello world Hellð ", 0, 30);
echo json_encode($myvariable);
I think the simplest solution would be to use mb_substr
Performs a multi-byte safe substr() operation based on number of characters.
php > $myvariable = array();
php > $myvariable['hello'] = mb_substr("Hello world Hello world Hellð ", 0, 30);
php > var_dump($myvariable);
array(1) {
["hello"]=>
string(33) "Hello world Hello world Hellð "
}
php > echo json_encode($myvariable);
{"hello":"Hello world Hello world Hell\ud83d\ude04 "}
php >
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With