Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP substring and strange icon on rendered html

I really dont know why on cyrillic font, substring replace some characters with "?"

My Code

$string1 = get_the_content();
$string = strip_tags($string1);
$stringcutted = substr($string,0,150);
$replacement = "...";
$final = substr($stringcutted, 0, -3).$replacement;

And look how it is rendered on html

strange icon1strange icon2

Any solution?

like image 318
Novkovski Stevo Bato Avatar asked May 23 '12 00:05

Novkovski Stevo Bato


1 Answers

Because PHP's string functions are based on strings of bytes; they have no knowledge of character encoding. So in something like UTF-8, where a character can take up more than one byte, it doesn't work the way you'd want it to:

<?php 
 $x = 'Подмосковные вечера';
 print(strlen($x)."\n");        # 37, not 19
 print(substr($x,0,1)."\n");    # �, not П
 print(substr($x,0,2)."\n");    # П, not По
?>

Look at the multibyte string functions if you want to manipulate non-ASCII text.

like image 96
Mark Reed Avatar answered Oct 13 '22 06:10

Mark Reed