正则表达式替换解决方案
<?php
$pattern = '/(?:^|\n)\s*\*[\t ]*([^\n\r]+)/';
$replacement = '<li>${1}</li>';
$count = 0;
$p = preg_replace($pattern, $replacement, $p, -1, $count);
if ($count > 0) {
$pattern = '/(<li>.*?(?:<\/li>)(?:\s*<li>.*?(?:<\/li>))*)/';
$replacement = '<ul>${1}</ul>';
$p = preg_replace($pattern, $replacement, $p);
}
?>
测试字符串
* Lorem ipsum dolor sit amet, * consec*tetur adipiscing elit
*Curabitur ullamcorper neque sit amet
* Pellente*sque nec quam rhoncus
Suspendisse ut lacinia arcu
* Nullam in vulpu*tate tellus
输出
<ul><li>Lorem ipsum dolor sit amet, * consec*tetur adipiscing elit</li>
<li>Curabitur ullamcorper neque sit amet</li>
<li>Pellentesque nec quam rhoncus</li></ul>
Suspendisse ut lacinia arcu
<ul><li>Nullam in vulputate tellus</li></ul>
PHP 手册 - preg_replace