[英]Parsing a mix of structured and unstructured text
我需要解析文本块,其格式如下:
Today the weather is excellent bla bla bla.
<temperature>35</temperature>.
I'm in a great mood today.
<item>Desk</item>
我想解析像这样的文本,并将其转换为类似这样的数组:
$array[0]['text'] = 'Today the weather is excellent bla bla bla. ';
$array[0]['type'] = 'normalText';
$array[1]['text'] = '35';
$array[1]['type'] = 'temperature';
$array[2]['text'] = ". I'm in a great mood today.";
$array[2]['type'] = 'normalText';
$array[3]['text'] = 'Desk';
$array[3]['type'] = 'item';
基本上,我希望数组包含与原始文本中相同顺序的所有文本,但分成类型:普通文本(意味着不在任何标签之间的东西),以及其他类型,如温度,项目,由文本之间的标签确定。
有没有办法做到这一点(即将文本分成普通文本和其他类型,使用正则表达式)或者我应该在幕后将文本转换为结构合理的文本,如:
<normal>Today the weather is excellent bla bla bla.</normal>
<temperature>35</temperature>.
<normal> I'm in a great mood today.</normal><item>Desk</item>
在它试图解析文本之前?
编辑:现在它完全按预期工作!
解:
<?php
$code = <<<'CODE'
Today the weather is excellent bla bla bla.
<temperature>35</temperature>.
I'm in a great mood today.
<item>Desk</item>
CODE;
$result = array_filter(
array_map(
function ($element) {
if (!empty($element)) {
if (preg_match('/^\<([^\>]+)\>([^\<]+)\</', $element, $matches)) {
return array('text' => $matches[2],
'type' => $matches[1]);
} else {
return array('text' => $element,
'type' => 'normal');
}
}
return false;
},
preg_split('/(\<[^\>]+\>[^\<]+\<\/[^\>]+\>)/', $code, null, PREG_SPLIT_DELIM_CAPTURE)
)
);
print_r($result);
输出:
Array
(
[0] => Array
(
[text] => Today the weather is excellent bla bla bla.
[type] => normal
)
[1] => Array
(
[text] => 35
[type] => temperature
)
[2] => Array
(
[text] => .
I'm in a great mood today.
[type] => normal
)
[3] => Array
(
[text] => Desk
[type] => item
)
)
尝试逐行阅读文本。 你有2个案例。 添加普通文本并添加具有特殊标记的文本。 将常规文本添加到变量时,请查找带有regexp的标记。
preg_match("/\<(\w)\>/", $line_from_text, $matches)
匹配标记,()保存单词以与$ matches中的数组一起使用。 现在只需将文本添加到变量中,直到遇到结束标记。 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.