Home >Backend Development >PHP Tutorial >Example analysis of strlen and mb_strlen usage in php

Example analysis of strlen and mb_strlen usage in php

高洛峰
高洛峰Original
2016-12-26 13:14:211487browse

This article analyzes the usage of strlen and mb_strlen in php with examples. Share it with everyone for your reference, the details are as follows:

First look at the following code (file encoding utf8):

<?php
$utf8_string = "abcd我你他她它";
var_dump(strlen($utf8_string));
var_dump(mb_strlen($utf8_string, &#39;gb2312&#39;));
var_dump(mb_strlen($utf8_string, &#39;utf8&#39;));
var_dump(mb_strlen($utf8_string));
?>

Running result:

int 19
 
int 13
 
int 9
 
int 19

When the second parameter is the same as the original encoding of the string, the real number of words in the sense of human understanding will be calculated. (Chinese characters only count once)

I hope this article will be helpful to everyone in PHP programming.

For more related articles on the usage examples of strlen and mb_strlen in PHP, please pay attention to 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