如何从php字符串中删除字符而不会干扰HTML标记

时间:2015-07-28 11:42:11

标签: php html

我有一个包含html标签的字符串,如下所示。

$desc = "<p>Lorem <strong>ipsum</strong> dolor sit amet</p><p>Duo at agam maiorum instructior, ut tale quidam ancillae qui, est cu paulo consetetur.</p>"

我想采取前10个字符:

  1. HTML标签不属于计数范围。
  2. 已打开的所有HTML标记均已正确关闭。
  3. 现在如果使用substr:

    $result = substr($desc, 0, 10);
    

    实际结果是:<p>Lorem <

    我想要的是:<p>Lorem <strong>ipsu</strong></p>

1 个答案:

答案 0 :(得分:0)

我通过使用非常好的代码实现了这一点 How to close unclosed HTML Tags?由kamal回答

<?php
 $str = "<p>Lorem <strong>ipsum</strong> dolor sit amet</p><p>Duo at agam maiorum instructior, ut tale quidam ancillae qui, est cu paulo consetetur.</p>";
 $s = strip_tags($str);
 $result = substr($s, 0, 10);
 $sarr = explode(' ', $result);
 $last = end($sarr);
 $l = strpos($str, $last);
 $r = substr($str, 0, $l);
 echo closetags($r.$last);

 function closetags ( $html )
    {
    #put all opened tags into an array
    preg_match_all ( "#<([a-z]+)( .*)?(?!/)>#iU", $html, $result );
    $openedtags = $result[1];
    #put all closed tags into an array
    preg_match_all ( "#</([a-z]+)>#iU", $html, $result );
    $closedtags = $result[1];
    $len_opened = count ( $openedtags );
    # all tags are closed
    if( count ( $closedtags ) == $len_opened )
    {
    return $html;
    }
    $openedtags = array_reverse ( $openedtags );
    # close tags
    for( $i = 0; $i < $len_opened; $i++ )
    {
        if ( !in_array ( $openedtags[$i], $closedtags ) )
        {
        $html .= "</" . $openedtags[$i] . ">";
        }
        else
        {
        unset ( $closedtags[array_search ( $openedtags[$i], $closedtags)] );
        }
    }
    return $html;
}
?>