我想检查一个字符串是否具有编号列表的格式以正确设置以下行的格式:
1. Something
Text
1.1. Anything
Text
应得
<h2>1. Something</h2>
Text
<h3>1.1. Anything</h3>
Text
等等...
我为preg_match尝试了类似的方法:
#([\d]*\.)*\s*\K(.+?)$#s
您可以使用:
$str = "1. Something\nText\n1.1. Anything\nText";
$result = preg_replace_callback('/^\d+\.((?:\d+\.)*)\h+.+$/m',
function($m) {$t=($m[1]!="")?'h3':'h2'; return "<$t>$m[0]</$t>";}, $str);
输出:
<h2>1. Something</h2>
Text
<h3>1.1. Anything</h3>
Text
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句