PHP substring and weird html rendered icon

I really don’t know why in Cyrillic font substring will 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 displayed in html

strange icon1strange icon2

Any solution?

+6
source share
3 answers

Because PHP string functions are based on byte strings; they do not know the character encoding. Thus, in something like UTF-8, where a character can take more than one byte, it does not work the way you would like:

 <?php $x = ' '; print(strlen($x)."\n"); # 37, not 19 print(substr($x,0,1)."\n"); #  , not  print(substr($x,0,2)."\n"); # , not  ?> 

See multibyte string functions if you want to manipulate text without ASCII.

+3
source

You need to check the character encoding. Basically, you have a string encoded in one format, and you duplicate it in another format.

For international things (and it looks like you are doing this), I would use UTF-8: - In your HTML file add to the head (at the top) - In your PHP, make sure that you treat all the lines as UTF-8 - If you have there is also a database, make sure that the database, tables and fields are configured as UTF-8 (warning: this change may damage the data extraction without import / export!). - If you are reading template files with special characters, make sure it is UTF-8. (If there are no special characters, then normal AscII will do)

This is a simple answer. There you can read about character encoding - just google.

(An alternative solution, use ASCII, but convert everything to web-based character codes. But it's harder to get all the codes in order, especially when processing user input.)

0
source

You need to manually determine the encoding. Use mb_substr (). This should help. http://php.net/manual/en/function.mb-substr.php

0
source

Source: https://habr.com/ru/post/916414/


All Articles