0

我正在运行一个 WordPress 插件,它处理内容并在 X 个段落之后插入一些内容。现在我的代码非常野蛮。样本:

$del = '</p>';
$para_count = substr_count($html, $del);

if($para_count == 0) {
    $del = "\r\n\r\n";
    $para_count = substr_count($html, $del); # echo "1";
}

if($para_count == 0) {
    $del = "\n\n\n\n";
    $para_count = substr_count($html, $del); # echo "2";
}

if($para_count == 0) {
    $del = '
';
    $para_count = round(substr_count($html, $del) / 2); # echo "3";
}       

if($para_count == 0) {
    $del = '<br/><br/>';
    $para_count = substr_count($html, $del); # echo "4";
} 

if($para_count == 0) {
    $del = '<br><br>';
    $para_count = substr_count($html, $del); # echo "5";
}

if($para_count == 0) {
    $del = '<br /><br />';
    $para_count = substr_count($html, $del); # echo "6";
}

if($para_count == 0) {
    return "<div class='display:none'><!-- NO DELIMETER FOUND --></div>".$html;
}

当来源未知时,找到段落分隔符的最佳方法是什么?我可以将我的脚本简化为可能更准确的更简单的代码吗?

4

0 回答 0