php将特定字符串包装在不同的标记中

php将特定字符串包装在不同的标记中,php,Php,我从服务器上得到一些文本,看起来像 Title || text text text text text text Title || text text text text text text text text text text text text 我需要添加不同的标签,使其看起来像 <div class="receipt__ingredients__table"> <div class="receipt__ingredients__table__row"><

我从服务器上得到一些文本,看起来像

Title ||
text text text
text text text

Title ||
text text text
text text text
text text text
text text text
我需要添加不同的标签,使其看起来像

<div class="receipt__ingredients__table">
<div class="receipt__ingredients__table__row"><p class="receipt__ingredients__title">Title</p></div>
<div class="receipt__ingredients__table__row"><p>text text text</p></div>
<div class="receipt__ingredients__table__row"><p>text text text</p></div>
</div>

<div class="receipt__ingredients__table">
    <div class="receipt__ingredients__table__row"><p class="receipt__ingredients__title">Title</p></div>
    <div class="receipt__ingredients__table__row"><p>text text text</p></div>
    <div class="receipt__ingredients__table__row"><p>text text text</p></div>
    <div class="receipt__ingredients__table__row"><p>text text text</p></div>
</div>

标题

文本文本文本

文本文本文本

标题

文本文本文本

文本文本文本

文本文本文本

下面是我的代码

$receipt_ingredients = "Title ||
                    text text text
                    text text text

                    Title ||
                    text text text
                    text text text
                    text text text
                    text text text";

$receipt_ingredients = preg_replace('/^(.*?)\s*[|]{2}/m', '<p class="receipt__ingredients__title">$1</p>', $receipt_ingredients);


$receipt_ingredients = '<div class="receipt__ingredients__table__row">'.str_replace(array("\r","\n\n","\n"),array('',"\n","</div>\n<div class='receipt__ingredients__table__row'>"),trim($receipt_ingredients,"\n\r")).'</div>';

echo $receipt_ingredients;
$receipt\u components=“Title||
文本文本文本
文本文本文本
头衔||
文本文本文本
文本文本文本
文本文本文本
“文本”;
$receipt_components=preg_replace('/^(.*)\s*[|]{2}/m','

$1

,$receipt_components); $receipt\u components=''.str\u replace(数组(“\r”、“\n\n”、“\n”)、数组(““\n”、“\n”)、修剪($receipt\u components、“\n\r”)); echo$receipt_配料;
但我得到的结构看起来像

 <div class="receipt__ingredients__table__row"><p class="receipt__ingredients__title">Title</p></div>
<div class="receipt__ingredients__table__row">text text text</div>
<div class="receipt__ingredients__table__row">text text text</div>
<div class="receipt__ingredients__table__row"><p class="receipt__ingredients__title">Title</p></div>
<div class="receipt__ingredients__table__row">text text text</div>
<div class="receipt__ingredients__table__row">text text text</div>
<div class="receipt__ingredients__table__row">text text text</div>
<div class="receipt__ingredients__table__row">text text text</div>  
标题

文本文本文本 文本文本文本 标题

文本文本文本 文本文本文本 文本文本文本 文本文本文本 如何获得所需的结构?

尝试使用explode()。首先按空行分解,然后按| |分解,最后按换行符分解

首先创建一个映射数组,类似于:

$exploded = array('blocks' => array(
0 => array(
    'title' => '',
    'text' => ''
),
1 => array(
    'title' => '',
    'text' => ''
)
));

// Explode and fill array
$exploded = array();
$blocks = explode('\r\n \r\n', $receipt_ingredients); // NOTE you have to check the newline char coming from the db and use that

foreach ( $blocks as $block ) {
    $parts = explode('||', $block);
    $block_array = array(
        'title' => $parts[0],
        'text' => count($parts) > 1 ? $parts[1] : ''
    );

    // You could also simply echo so you do not have to reiterate the array again

    $exploded[] = $block_array;
}

这是一种方法,通常我会在这种情况下使用这种方法。

E\u过多\u TEXT\u TEXT\u TEXT为什么要从服务器获取这种格式的文本?我不会用正则表达式做这种事!