str_split() Function

It returns an array containing parts of the string.

Syntax

str_split(string, length);

Parameters

  • string: It specifies the string to be checked, it can also be a variable name of type string.
  • length: It specifies the length of each part of the string to be stored in the string, by default, it is 1. If the length is larger than the size of the string, then the complete string is returned.

Example: This example illustrates the basic implementation of the str_split() Function in PHP.

PHP




<?php
 
$str = "w3wiki";
print_r(str_split($str));
echo "<br>";
print_r(str_split($str, 3));
 
?>


Output:

Array ( 
[0] => G
[1] => e
[2] => e
[3] => k
[4] => s
[5] => F
[6] => o
[7] => r
[8] => G
[9] => e
[10] => e
[11] => k
[12] => s
)
Array (
[0] => Gee
[1] => ksF
[2] => orG
[3] => eek
[4] => s
)

Explain some string functions of PHP

In the programming world, a string is considered a data type, which in general is a sequence of multiple characters that can contain whitespaces, numbers, characters, and special symbols. For example, “Hello World!”, “ID-34#90” etc. PHP also allows single quotes(‘ ‘) for defining a string. Every programming language provides some in-built functions for the manipulation of strings. Some of the basic string functions provided by PHP are as follows:

Similar Reads

strlen() Function

It returns the length of the string i.e. the count of all the characters in the string including whitespace characters....

strrev() Function

...

trim(), ltrim(), rtrim(), and chop() Functions

It returns the reversed string of the given string....

strtoupper() and strtolower() Function

...

str_split() Function

It removes white spaces or other characters from the string. They have two parameters: one string and another charList, which is a list of characters that need to be omitted....

str_word_count() Function

...

strpos() Function

It returns the string after changing the cases of its letters....

str_replace() Function

...

ucwords() Function

It returns an array containing parts of the string....