en_btr_backend/app/Handlers/LexiconEntryTrait.php

39 lines
809 B
PHP

<?php
namespace App\Handlers;
/**
* LexiconEntryTrait.php
*
* @author: Leonard Smith <leonard@acornwebconsultants.com>
* Date: 10/6/20
* Time: 8:34 PM
*/
trait LexiconEntryTrait
{
/**
* Extract the lexeme and the commentary portion into separate variables
*
* @param $entry
* @return array
*/
public function parseEntry($entry)
{
$lexeme = '';
$commentary = '';
$n = 0;
foreach(preg_split('~[\r\n]+~', $entry) as $line){
if(empty($line) or ctype_space($line)) continue; // skip only spaces
if ($n === 0) {
$lexeme = ltrim($line, '#');
} else {
$commentary .= $line . "\n";
}
$n++;
}
return [$lexeme, $commentary];
}
}