I have a string like this:
$string = '"Joe Bob" "John Doe" "Amber Page" "Jordan Tole"';
I want to store it in an array with each item in between the " " being an element inside of the array.
e.g.
$Array = StringToArray($string);
I have a string like this:
$string = '"Joe Bob" "John Doe" "Amber Page" "Jordan Tole"';
I want to store it in an array with each item in between the " " being an element inside of the array.
e.g.
$Array = StringToArray($string);
Using a combination of PHP's explode function and substr you could use to build an array of the names like this:
$string = '"Joe Bob" "John Doe" "Amber Page" "Jordan Tole"';
$string = substr($string,1,-1);
$names = explode('" "', $string);
This would output the following:
Array
(
[0] => Joe Bob
[1] => John Doe
[2] => Amber Page
[3] => Jordan Tole
)
You can make use of preg_split to pull the string apart:
$string = '"Joe" "John" "Amber" "Jordan"';
$names = preg_split('~(^"|" "|"$)~', $string, 0, PREG_SPLIT_NO_EMPTY);
Result:
array(4) {
[0]=> string(3) "Joe"
[1]=> string(4) "John"
[2]=> string(5) "Amber"
[3]=> string(6) "Jordan"
}
Pending you correct the string above..
$string = '"Joe" "John" "Amber" "Jordan"';
using explode. <?php $arr = explode('" "', $string); will provide you the following: $arr = array( 0 => '"Joe"', 1 => '"John"'...
However if you meant:
<?php
$string = "Joe John Amber Jordan";
$arr = explode(' ', $string);
// will provide the samething without the " and ".
$arr = explode('" "', trim($string, '"'));