Convert string to array php

how to convert a string in array in php i.e

$str="this is string";

should be like this

  arr[0]=this
  arr[1]=is
  arr[2]=string 

The str_split($str, 3); splits the string in 3 character word but I need to convert the string after whitespace in an array.

Convert string to array php

asked Mar 5, 2011 at 12:59

1

Use explode function

$array = explode(' ', $string);

The first argument is delimiter

Convert string to array php

answered Mar 5, 2011 at 13:02

decezedeceze

497k81 gold badges719 silver badges867 bronze badges

0

try json_decode like so


answered May 23, 2017 at 4:46

Convert string to array php

2

Take a look at the explode function.


answered Mar 5, 2011 at 13:04

CodemwnciCodemwnci

53.1k10 gold badges96 silver badges129 bronze badges


Convert string to array php

Zeeshan

1,65912 silver badges16 bronze badges

answered Mar 26, 2014 at 6:53

RavindraRavindra

1,8413 gold badges16 silver badges20 bronze badges

1

explode() might be the function you are looking for

$array = explode(' ',$str);

answered Mar 5, 2011 at 13:03

AndreasAndreas

5,2774 gold badges40 silver badges59 bronze badges

There is a function in PHP specifically designed for that purpose, str_word_count(). By default it does not take into account the numbers and multibyte characters, but they can be added as a list of additional characters in the charlist parameter. Charlist parameter also accepts a range of characters as in the example.

One benefit of this function over explode() is that the punctuation marks, spaces and new lines are avoided.

$str = "1st example:
        Alte Füchse gehen schwer in die Falle.    ";

print_r( str_word_count( $str, 1, '1..9ü' ) );

/* output:
Array
(
    [0] => 1st
    [1] => example
    [2] => Alte
    [3] => Füchse
    [4] => gehen
    [5] => schwer
    [6] => in
    [7] => die
    [8] => Falle
)
*/

answered May 27, 2015 at 14:41

DanijelDanijel

12.1k4 gold badges36 silver badges54 bronze badges

explode — Split a string by a string

Syntax :

array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )
  • $delimiter : based on which you want to split string
  • $string. : The string you want to split

Example :

// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

In your example :

$str = "this is string"; 
$array = explode(' ', $str);

answered Aug 15, 2018 at 12:59

Convert string to array php

Amitesh BhartiAmitesh Bharti

11.8k6 gold badges57 silver badges54 bronze badges

You can achieve this even without using explode and implode function. Here is the example:

Input:

This is my world

Code:

$part1 = "This is my world"; 
$part2 = str_word_count($part1, 1);

Output:

Array( [0] => 'This', [1] => 'is', [2] => 'my', [3] => 'world');

answered Jan 30, 2018 at 10:57

Convert string to array php

1

here, Use explode() function to convert string into array, by a string

click here to know more about explode()

$str = "this is string";
$delimiter = ' ';  // use any string / character by which, need to split string into Array
$resultArr = explode($delimiter, $str);  
var_dump($resultArr);

Output :

Array
(
    [0] => "this",
    [1] => "is",
    [2] => "string "
)

it is same as the requirements:

  arr[0]="this";
  arr[1]="is";
  arr[2]="string";

answered May 21, 2018 at 12:09

Gautam RaiGautam Rai

2,3552 gold badges19 silver badges31 bronze badges

1

Can you convert a string to an array?

We can convert a String to a character array using either a naive for loop or the toCharArray() method. To convert a string to a string array based on a delimeter, we can use String. split() or Pattern.

How do I convert a string to an array of elements?

Approach:.
Convert the given string into set of string..
Now create an empty string array..
Convert the string set to Array of string using set. toArray() by passing an empty array of type string..
Print the array of string..

How do I convert a string to PHP?

Answer: Use the strval() Function You can simply use type casting or the strval() function to convert an integer to a string in PHP.

What does array () do in PHP?

An array is a special variable that we use to store or hold more than one value in a single variable without having to create more variables to store those values. To create an array in PHP, we use the array function array( ) . By default, an array of any variable starts with the 0 index.