How to know the length of string in php

醉折花枝作酒筹
Release: 2023-03-11 22:24:01
Original
6164 people have browsed it

In the previous article, we learned about the method of using the "natural" algorithm to know the similarities and differences of strings. If you need it, please read " How to compare the similarities and differences of strings with the "natural" algorithm in php (case sensitive) 》. This time we will introduce to you the method of obtaining the length of a string. You can refer to it if you need it.

In php, we need to know the length of the string so that we can do some things and understand the string. So without further ado, let’s take a look at how to obtain string functions.

First let’s look at a small example.

<?php
echo strlen("World grand, welcome home!");
?>
Copy after login

The result of this small example is

How to know the length of string in php

When I saw this answer, I wonder if my friends have put this string from the beginning Count to the end. Let me start by saying that you must count it once. What if this function is wrong? Then I feel so accomplished. But I counted from beginning to end and found that the length of this function is really 26.

There is no problem with this function, so let’s talk about this function.

strlen() function returns the length of the string (number of bytes). strlen() is often used in loops and other functions when it is important to determine when a string ends. (For example, in a loop, we need to end the loop after the last character in the string)

Let's look at another example.

<?php
echo strlen("世界盛大,欢迎回家!");
?>
Copy after login

The result is

How to know the length of string in php

I found that the answer is different from the above example, right? One parameter is in Chinese and one parameter is in English. Is there such a big difference? Chinese characters plus punctuation marks are obviously only 10 characters long. Why is the length of this one really 30 characters? Have you ever thought about it?

Of course there is a big difference between Chinese words and English words. And the bytes occupied by each letter are different under various encodings

Under theASCII code, one English letter (regardless of uppercase and lowercase) occupies one byte of space, and one Chinese characters occupy two bytes of space. A sequence of binary numbers, used as a digital unit in the computer, is generally an 8-bit binary number, converted to decimal. The minimum value is -128 and the maximum value is 127. For example, an ASCII code is a byte.

Under UTF-8 encoding, one English character is equal to one byte, and one Chinese character (including traditional Chinese) is equal to three bytes. Chinese punctuation occupies three bytes, and English punctuation occupies one byte.

Under Unicode encoding, one English word is equal to two bytes, and one Chinese character (including traditional Chinese) is equal to two bytes. Chinese punctuation occupies two bytes, and English punctuation occupies two bytes.

The encoding used in our example is utf-8, so one Chinese character occupies three bytes. So the length of 10 Chinese characters is up to 30.

That’s all. If you want to know anything else, you can click here. → →php video tutorial

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

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!