JBZoo\Utils\Str::parseLines PHP Method

parseLines() public static method

Parse text by lines
public static parseLines ( string $text, boolean $toAssoc = true ) : array
$text string
$toAssoc boolean
return array
    public static function parseLines($text, $toAssoc = true)
    {
        $text = htmlspecialchars_decode($text);
        $text = self::clean($text, false, false);
        $text = str_replace(array("\n", "\r", "\r\n", PHP_EOL), "\n", $text);
        $lines = explode("\n", $text);
        $result = array();
        if (!empty($lines)) {
            foreach ($lines as $line) {
                $line = trim($line);
                if ($line === '') {
                    continue;
                }
                if ($toAssoc) {
                    $result[$line] = $line;
                } else {
                    $result[] = $line;
                }
            }
        }
        return $result;
    }

Usage Example

Example #1
0
 public function testParseLines()
 {
     isSame(array('asd'), Str::parseLines('asd', false));
     isSame(array('asd' => 'asd'), Str::parseLines('asd', true));
     isSame(array('asd' => 'asd'), Str::parseLines('asd'));
     $lines = array('', false, 123, 456, ' 123   ', '      ', 'ASD', '0');
     isSame(array('123' => '123', '456' => '456', 'ASD' => 'ASD', '0' => '0'), Str::parseLines(implode("\r", $lines), true));
     isSame(array('123' => '123', '456' => '456', 'ASD' => 'ASD', '0' => '0'), Str::parseLines(implode("\n", $lines), true));
     isSame(array('123', '456', '123', 'ASD', '0'), Str::parseLines(implode("\r\n", $lines), false));
 }
All Usage Examples Of JBZoo\Utils\Str::parseLines