Home  >  Article  >  Backend Development  >  php string

php string

不言
不言Original
2018-04-23 16:32:011100browse

This article introduces the content of PHP strings, which has certain reference value. Now I share it with everyone. Friends in need can refer to

iconv_strlen to get the number of characters
// Assume that the current page encoding is GBK

<?php
$str="中国abc";
echo strlen($str); //返回7,因为 GBK编码每个中文两个字节,strlen是返回字符串所占的字节长度。
echo "<hr>";
echo iconv_strlen($str,"GBK"); //返回5.iconv_strlen 是统计字符串的字符数量
?>

//Assume that the current page encoding is UTF-8

<?php
$str="中国abc";
echo strlen($str); //返回9,因为 UTF-8编码每个中文三个字节,strlen是返回字符串所占的字节长度。
echo "<hr>";
echo iconv_strlen($str,"UTF-8"); //返回5.iconv_strlen 是统计字符串的字符数量
?>

iconv_strlen can calculate the accurate number of characters no matter what encoding it is. number.

mb_strlen — Get the length of a string
PHP’s built-in string length function strlen cannot handle Chinese strings correctly. It only gets the number of bytes occupied by the string.
For GB2312 Chinese encoding, the value obtained by strlen is 2 times the number of Chinese characters, while for UTF-8 encoded Chinese, the difference is 3 times (under UTF-8 encoding, one Chinese character occupies 3 characters Festival).
Using the mb_strlen function can better solve this problem.
The usage of mb_strlen is similar to strlen, except that it has a second optional parameter to specify the character encoding.
For example, to get the UTF-8 string $str length, you can use mb_strlen($str,'UTF-8').
If the second parameter is omitted, PHP's internal encoding will be used. The internal encoding can be obtained through the mb_internal_encoding() function.
It should be noted that mb_strlen is not a core function of PHP. Before using it, you need to make sure that php_mbstring.dll is loaded in php.ini, that is, make sure that the line "extension=php_mbstring.dll" exists and has not been commented out, otherwise it will An undefined function problem occurred.
mb_strlen('abcdef','utf-8') and mb_strlen('one two three four five six', 'utf-8') are both equal to 6

trim
echo trim(" ,1,2,3,4,", ","); // You will get 1,2,3,4 with the "," numbers at both ends cut off.

strcmp() function compares two strings.
Note: The strcmp() function is binary safe and case-sensitive.
This function returns:
0 - If the two strings are equal
4c811dccd30495ef030c72eb632c041c0 - If string1 is greater than string2

HTML related
1,htmlspecialchars($string)
This is its simplest usage, converting some special characters (as the name suggests) &,',"6580843315dd7804e35fd3743df832ea in the string into their corresponding HTML entity forms:
PHP:

  $str = "i love kiki, iwind said.";
    echo htmlspecialchars($str);

will output
i love 8121d6c8a34839335b283c3765c68ed6kikie6e38b3c62e8df885fe2e3986461aa63, iwind said.
2,htmlentities($string )
Convert all characters that can be converted into entity form into entity form.
3,html_entity_decode($string);
Added after PHP4.3.0, it has the opposite function to htmlentities($string).
4,nl2br($string)
Convert all newline characters in the string into
newline characters. For example:

 PHP:
    $str = "i love kiki,/n iwind said.";
    echo nl2br($str);

will output
i love kiki,
iwind said.

Related recommendations:

Detailed explanation of PHP string encoding issues


##

The above is the detailed content of php string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn