All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Split String By Character

Last Updated : Mar 11, 2024

PHP Split String By Character

In this article we will show you the solution of PHP split string by character, one of the fundamental operations in any programming language is splitting a string. To handle split relation activities, PHP comes with a number of built-in functions.

When we learn the term "split," we will see how one string is broken up into several strings.

Depending on our business needs, we can separate a string in a variety of ways.

In other words, if you wish to separate a string into its individual characters, you can do it by space, special character, etc.

While addressing the string split, we can also use the size combination. We'll discuss the concept of splitting a string of PHP code by characters.

Step By Step Guide On PHP Split String By Character :-

Method - Using the str_split() method, split a string

<?php
$string = "Welcome to the Talkerscode ";
echo "Actual String:\n";
echo $string;
echo "\n\n>";
echo " After string, an array of string str_split:\n";
$arrays = str_split($string);
print_r($arrays);
?>
  1. As you can see, we use the str_split() method to write the PHP code necessary to split the string into individual characters.
  2. We initialise the string variable "$string" with the text "Welcome to the Talkerscode" at the beginning of the code.
  3. Then, on a new line, we print the text "Actual String:".
  4. It then uses the "echo" statement to print the value of the "$string" variable on the next line.
  5. After that, a new line is created and the string " After string, an array of string str_split:" is printed.
  6. After invoking the "str_split()" method to separate the string into an array of distinct characters, the result is saved in the "$arrays" variable.
  7. The "print_r()" function, which presents the array's values in a readable format, is used to print the contents of the "$arrays" variable in the last statement.
  8. This enables us to view the outcome of character-by-character segmenting the original string.

Method - Using the str_split() function, divide a string of the specified length.

<?php
$string = "Welcome to the World";
echo "Actual String:\n";
echo $string;
echo "\n\n";
echo "" After string, an array of string str_split:\n";
$arrays = str_split($string,4);
print_r($arrays);
?>
  1. You can see that we have written PHP code that divides a string into characters according to its length using the str_split() method.
  2. The string variable "$string" is initialised with the text "Welcome to the Talkerscode" at the beginning of the programme.
  3. Then, using the "echo" statement, the string "Actual String:" is printed on a new line, followed by the value of the "$string" variable on the following line.
  4. Then, on a new line, the text "Array of string after string str_split:" is printed.
  5. The "str_split()" method is then invoked with the string to split and the size of each block as parameters.
  6. In this instance, the length of each chunk is set to 4 characters.
  7. The "$arrays" variable holds the array of resultant substrings.
  8. The "print_r()" function, which presents the array's values in a readable format, is used to print the contents of the "$arrays" variable in the last statement.
  9. This enables us to view the outcome of dividing the primary string into 4-character-long substrings.

Conclusion :-

As a result, we have successfully learnt how to split strings in PHP by characters.

We also learned how to divide a string into substrings using the "str_split()" function.

While the second code divides the text into substrings of a certain length, the first code divides the string into an array of individual characters.

I hope this article on PHP split string by character helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪