Home  >  Article  >  Backend Development  >  PHP string learning: Divide strings into substrings of smaller lengths

PHP string learning: Divide strings into substrings of smaller lengths

青灯夜游
青灯夜游Original
2021-08-11 12:46:223569browse

In the previous article "PHP String Learning: Reverse Output All Characters", we reversed the string and output all the characters in the string in reverse order. This time we continue to learn PHP strings and learn about the methods of splitting strings and dividing strings into smaller substrings. You can refer to it if necessary.

As the title says, the theme of this article is to split the string and divide the string into smaller substrings. The substring can contain only one character, that is, split the string into its individual characters; it can also contain multiple characters, then the string can be split into multiple words.

So how to implement these two operations? Let's introduce it in detail through code examples.

First look at the following example:

<?php
$string = "Hello world";
var_dump(str_split($string));
?>

Output result:

PHP string learning: Divide strings into substrings of smaller lengths

##It can be seen that # is used in the above example ##str_split($string)

Split the string $string into characters and pass them into the array as elements of the array.

str_split( $string, $length )

The function can actually set two parameters. The optional parameter $length is used to set the length of each array element. The default value is 1 . If the $length parameter is omitted, it will be divided into characters as in the above example. If the $length parameter is set, the string will be split into several substrings (containing multiple characters) containing the given length character length. Let’s take a look at the following example:

<?php
$string = "Hello world";
var_dump(str_split($string,3));
?>

Output result:

PHP string learning: Divide strings into substrings of smaller lengthsAs can be seen,

str_split($string,3 )

Split the string $string into 3 substrings containing 3 characters and 1 substring containing 2 characters (because the original string character length is not enough). Note: If the $length parameter value is less than 1, the str_split() function will return FALSE; and if the $length parameter value is greater than the length of the $string string, the entire string $string will be used as the only element of the array. return.

<?php
$string = "Hello world";
var_dump(str_split($string,12));
?>

Output result:

PHP string learning: Divide strings into substrings of smaller lengthsOkay, that’s all. If you want to know anything else, you can click this. → →

php video tutorial

Finally, I recommend reading a classic course "

PHP String Processing (Jade Girl Heart Sutra Edition)

", it's free~ come and learn !

The above is the detailed content of PHP string learning: Divide strings into substrings of smaller lengths. 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