php string

不言
Release: 2023-03-24 18:00:01
Original
1139 people have browsed it

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 是统计字符串的字符数量
?>
Copy after login

//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 是统计字符串的字符数量
?>
Copy after login

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
<0 - If string1 is less than string2
>0 - If string1 is greater than string2

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

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

will output
i love kiki, 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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template