簡體   English   中英

PHP substr但保留HTML標簽?

[英]PHP substr but keep HTML tags?

我想知道是否有一種優雅的方式來修剪一些文本但是在識別HTML標簽的同時?

例如,我有這個字符串:

$data = '<strong>some title text here that could get very long</strong>';

並且假設我需要在頁面上返回/輸出此字符串,但希望它不超過X個字符。 讓我們說35這個例子。

然后我用:

$output = substr($data,0,20);

但現在我最終得到:

<strong>some title text here that 

正如您所看到的那樣,關閉強標簽將被丟棄,從而打破HTML顯示。

有沒有解決的辦法? 另請注意,可以在字符串中包含多個標記,例如:

<p>some text here <strong>and here</strong></p>

幾個mounths之前我創建了一個特殊功能,它可以解決您的問題。

這是一個功能:

function substr_close_tags($code, $limit = 300)
{
    if ( strlen($code) <= $limit )
    {
        return $code;
    }

    $html = substr($code, 0, $limit);
    preg_match_all ( "#<([a-zA-Z]+)#", $html, $result );

    foreach($result[1] AS $key => $value)
    {
        if ( strtolower($value) == 'br' )
        {
            unset($result[1][$key]);
        }
    }
    $openedtags = $result[1];

    preg_match_all ( "#</([a-zA-Z]+)>#iU", $html, $result );
    $closedtags = $result[1];

    foreach($closedtags AS $key => $value)
    {
        if ( ($k = array_search($value, $openedtags)) === FALSE )
        {
            continue;
        }
        else
        {
            unset($openedtags[$k]);
        }
    }

    if ( empty($openedtags) )
    {
        if ( strpos($code, ' ', $limit) == $limit )
        {
            return $html."...";
        }
        else
        {
            return substr($code, 0, strpos($code, ' ', $limit))."...";
        }
    }

    $position = 0;
    $close_tag = '';
    foreach($openedtags AS $key => $value)
    {   
        $p = strpos($code, ('</'.$value.'>'), $limit);

        if ( $p === FALSE )
        {
            $code .= ('</'.$value.'>');
        }
        else if ( $p > $position )
        {
            $close_tag = '</'.$value.'>';
            $position = $p;
        }
    }

    if ( $position == 0 )
    {
        return $code;
    }

    return substr($code, 0, $position).$close_tag."...";
}

這是演示: http//sandbox.onlinephpfunctions.com/code/899d8137c15596a8528c871543eb005984ec0201 (單擊“執行代碼”以檢查其工作原理)。

使用@newbieuser他的功能,我有同樣的問題,如@巴勃羅-帕索斯,這是(不)(在我的情況時突破極限$掉進一個html標簽<br />在R)

修復了一些代碼

if ( strlen($code) <= $limit ){
    return $code;
}

$html = substr($code, 0, $limit);       

//We must find a . or > or space so we are sure not being in a html-tag!
//In my case there are only <br>
//If you have more tags, or html formatted text, you must do a little more and also use something like http://htmlpurifier.org/demo.php

$_find_last_char = strrpos($html, ".")+1;
if($_find_last_char > $limit/3*2){
    $html_break = $_find_last_char;
}else{
    $_find_last_char = strrpos($html, ">")+1;
    if($_find_last_char > $limit/3*2){ 
        $html_break = $_find_last_char;
    }else{
        $html_break = strrpos($html, " ");
    }
}

$html = substr($html, 0, $html_break);
preg_match_all ( "#<([a-zA-Z]+)#", $html, $result );
......

substr(strip_tags($ content),0,100)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM