To Convert String to Array in PHP Without Explode we use for loop to count the number of words on the sentence. Create a blank array to store the words which we extract using a loop in PHP.

Convert String to Array in PHP Without Explode
Convert String to Array in PHP Without Explode

Here is the complete example how we Convert String to Array in PHP Without Explode,

Output:

Array
(
[0] => This
[1] => is
[2] => the
[3] => string
[4] => to
[5] => array
)

Code Explanations:

  • Here we first define the string, a blank variable, and a blank array.
  • A blank variable to store the extracted words from the defined string.
  • Blank array to store the words which we take from the word variable and convert to an array.
  • To extract the array we use for loop which loop through till the length of the string and check the spaces between the sentence.
  • After taking all word into an array we print using print_r() function to print an array which looks like the above output.

How to convert string to list in PHP

To convert string to list in PHP, we use the same as above but we use list() function. To know more about list() the function see details https://www.php.net/manual/en/function.list.php

In this process we do below steps,

  • Define a string.
  • Convert that string to an array.
  • That converted array changed to array list or simply list using PHP list() function.

Example to convert string to list in PHP

Complete code look like this,

Output:

How to convert string to list in PHP
How to convert string to list in PHP

You can check the above complete code using our code editor online, https://codingtasks.net/php-online-editor/

If you have any doubt on this, please comment below.

Also Check:

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

2 thoughts on “Convert String to Array in PHP Without Explode”

Leave a Reply

Your email address will not be published. Required fields are marked *