function.text2array.php
Convert a block of text to a trimmed, filtered array.
Indepdent of the line endings used.
- Author: Aidan Lister <aidan@php.net>
- Version: 1.1.0
- Link: http://aidanlister.com/repos/v/function.text2array.php
- Views: 5458
- Downloads: 487
Source
<?php
/**
* Convert a block of text to a trimmed, filtered array.
*
* Indepdent of the line endings used.
*
* @author Aidan Lister <aidan@php.net>
* @version 1.1.0
* @link http://aidanlister.com/repos/v/function.text2array.php
* @param string $text Text
*/
function text2array($text)
{
$array = preg_split('#(\r\n?|\n)#', $text, -1, PREG_SPLIT_NO_EMPTY);
$array = array_map('trim', $array);
return $array;
}
?>
Example
<pre>
<?php
require_once 'function.text2array.php';
// The user is asked to provide a list of search keywords (for example)
$userinput = 'a
list
of
keywords
to be used';
// Explode
$keywords = text2array($userinput);
// Display list of keywords
print_r($keywords);
?>
</pre>
Output
Array
(
[0] => a
[1] => list
[2] => of
[3] => keywords
[4] => to be used
)
Comments