次のような形式のテキスト ブロックを解析する必要があります。
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>
テキストを解析しようとする前に?