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

高洛峰
Release: 2023-03-04 09:52:02
Original
1494 people have browsed it

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));
?>
Copy after login

Running result:

int 19
 
int 13
 
int 9
 
int 19
Copy after login

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!

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