在标签中包装php字符串

I have this string extracted from my database

[###TITLE###] 
 Passed in 12%. 
[###TITLE###] 
1 to 2% dearer. Maintained throughout the day. 
[###TITLE###] 
 18.5m finer 2 to 3% dearer. 19.0m coarser 1 to 2% dearer. 
[###TITLE###] 
 19.5m finer 1 to 2% dearer. 20.0m coarser firm.

My question is: How can I wrap just the text between ###] and [###
( I want to wrap in a div the text without [### TITLE ###]

P.S: The whole text is just a string.

Expected Output

[###TITLE###] 
 Passed in 12%. 
[###TITLE###] 
 <div class="text">1 to 2% dearer. Maintained throughout the day.</div>
[###TITLE###] 
 <div class="text">18.5m finer 2 to 3% dearer. 19.0m coarser 1 to 2% dearer.</div> 
[###TITLE###] 
 <div class="text">19.5m finer 1 to 2% dearer. 20.0m coarser firm.</div>

How can I extract from the database in one variable just [### TITLE ###] and in another variable the rest of the text.

You can use regular expression based string replacement:

<?php

$input = <<< EOT
[###TITLE###]
 Passed in 12%.
[###TITLE###]
1 to 2% dearer. Maintained throughout the day.
[###TITLE###]
 18.5m finer 2 to 3% dearer. 19.0m coarser 1 to 2% dearer.
[###TITLE###]
 19.5m finer 1 to 2% dearer. 20.0m coarser firm.
EOT;

$output = preg_replace(
    '/(\])\s+([^\[]*)\s+(\[)?/m', 
    "\\1
<div class=\"text\">\\2</div>
\\3", 
    $input
);

var_dump($output);

The output of this is:

string(323) "[###TITLE###]
<div class="text">Passed in 12%. </div>
[###TITLE###]
<div class="text">1 to 2% dearer. Maintained throughout the day. </div>
[###TITLE###]
<div class="text">18.5m finer 2 to 3% dearer. 19.0m coarser 1 to 2% dearer. </div>
[###TITLE###]
<div class="text">19.5m finer 1 to 2% dearer. 20.0m coarser firm.</div>
"

Note: take care that there is a trailing white space at the end of the input, otherwise the expression will deliver a slightly different output. If in doubt simply append a single blank character to the input ;-)


Another approach, in my eyes much more flexible, is a constructive creation of the desired output. For that you first use a regular expression based multi line capture. Then you can craft the output exactly as you like:

<?php

$input = <<< EOT
[###TITLE###]
 Passed in 12%.
[###TITLE###]
1 to 2% dearer. Maintained throughout the day.
[###TITLE###]
 18.5m finer 2 to 3% dearer. 19.0m coarser 1 to 2% dearer.
[###TITLE###]
 19.5m finer 1 to 2% dearer. 20.0m coarser firm.
EOT;

preg_match_all('/(\\[.+\\]\s+)([^\[]*\s*)\s/', $input, $tokens);

$output = '';
for($i=0; $i<count($tokens[0]); $i++) {
    $output .= sprintf("%s<div class=\"text\">%s</div>
", $tokens[1][$i], $tokens[2][$i]);
}

var_dump($output);

The output here is again:

string(330) "[###TITLE###]
 <div class="text">Passed in 12%. </div>
[###TITLE###]
<div class="text">1 to 2% dearer. Maintained throughout the day. </div>
[###TITLE###]
 <div class="text">18.5m finer 2 to 3% dearer. 19.0m coarser 1 to 2% dearer. </div>
[###TITLE###]
 <div class="text">19.5m finer 1 to 2% dearer. 20.0m coarser firm.</div>
"

Warning: in both cases to have to make sure that the actual title and content lines to not contain square brackets apart from those actually delimiting the title. Handling such cases is possible, but makes things much more complex.