<?php
$output = listelements();
$array = explode("<li>", $output);
//First element will be empty, so remove it
unset($array[0]);
// Now remove "</li>" at end of input
array_walk($array, create_function('&$val', '$val = str_replace("</li>", "", $val)'));
// $array should now contain your elements
Answer from iblue on Stack OverflowConvert list into an array using php. How? - Stack Overflow
How can I turn a list of items into a php array? - Stack Overflow
Transform php array into a list of values - Stack Overflow
PHP convert a string array list to Array - Stack Overflow
Is the Text List To Array [ ] free to use?
Is the Text List To Array [ ] completely free?
How accurate is the Text List To Array [ ]?
<?php
$output = listelements();
$array = explode("<li>", $output);
//First element will be empty, so remove it
unset($array[0]);
// Now remove "</li>" at end of input
array_walk($array, create_function('&$val', '$val = str_replace("</li>", "", $val)'));
// $array should now contain your elements
explode won't do the trick nicely for html tags (considering them as multiple delimiters).
if CPU time is not a concern, try using preg_match, example below:
<?PHP
$input='<li>text1</li><li>text2</li><LI><p>text3</p></lI><Li>text fou4r</li>';
preg_match_all('(<(li|Li|LI|lI)>(.*)</(li|Li|LI|lI)>)siU', $input, $output);
print_r($output[2]);
?>
output:
Array
(
[0] => text1
[1] => text2
[2] => <p>text3</p>
[3] => text fou4r
)
If you mean:
word1 word2 word3
Then you want
$array = explode(' ', "word1 word2 word3");
If you actually mean "word 1 word 2 word 3", with numbers inbetween, then you'll need to get a little fancier with preg_split()
if you have all your words in a text file, one per line you can use the file function which will read the whole file into an array, one item per line
http://nz.php.net/manual/en/function.file.php
$str = 'answers[0] = "Love it"; answers[1] = "Hate it."; answers[2] = "Doesnt care"; answers[3] = "Like it";';
preg_match_all('/"(.*)";/U', $str, $matches);
$arr = $matches[1];
var_dump($arr);
Use
preg_split
to select just the values and put it in to an array
example:
$keywords = preg_split("/[\s,]+/", $str);
Just change the pattern!
check the documentation: http://php.net/manual/en/function.preg-split.php
https://arraythis.com/
Why this? Because I needed a quick tool for passing portions of data (data excel columns, text lists, etc.) to array. Hope it helps you too.