修改php限制文本函数,为其添加某种偏移量

web*_*ers 5 php

也许你们可以提供帮助:

我有一个名为$ bio的生物数据变量.

$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the author of the question";
Run Code Online (Sandbox Code Playgroud)

我搜索使用一组功能来搜索某个词的$生物,可以说,"作者"增加了围绕这个词的跨度类,我也得到:

$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the <span class=\"highlight\">author</span> of the question";
Run Code Online (Sandbox Code Playgroud)

我使用函数将文本限制为85个字符:

$bio = limit_text($bio,85);
Run Code Online (Sandbox Code Playgroud)

问题是当有字以前多则80个字符"作者"$生物.当limit_text()应用时,我将看不到突出显示的单词作者.

我需要的是limit_text()函数正常工作,最后添加包含span类高亮显示的所有单词.像这样的东西:

*"This is the limited text to 85 chars, but there are no words with the span class highlight so I am putting to be continued ... **author**, **author2** (and all the other words that have a span class highlight around them separate by comma "*
Run Code Online (Sandbox Code Playgroud)

希望你理解我的意思,如果没有,请评论,我会试着更好地解释.

这是我的limit_text()功能:

function limit_text($text, $length){ // Limit Text
        if(strlen($text) > $length) {
        $stringCut = substr($text, 0, $length);
        $text = substr($stringCut, 0, strrpos($stringCut, ' '));
        }
        return $text;
    }
Run Code Online (Sandbox Code Playgroud)

更新:

$xturnons = str_replace(",", ", ", $xturnons);
$xbio = str_replace(",", ", ", $xbio);

$xbio = customHighlights($xbio,$toHighlight); 
$xturnons = customHighlights($xturnons,$toHighlight);

$xbio = limit_text($xbio,85);
$xturnons = limit_text($xturnons,85);
Run Code Online (Sandbox Code Playgroud)

customHighlights添加span类的功能突出显示:

function addRegEx($word){ // Highlight Words
        return "/" . $word . '[^ ,\,,.,?,\.]*/i';
    }
    function highlight($word){
        return "<span class='highlighted'>".$word[0]."</span>";
    }
    function customHighlights($searchString,$toHighlight){
        $searchFor = array_map('addRegEx',$toHighlight);
        $result = preg_replace_callback($searchFor,'highlight',$searchString);
        return $result;
    }
Run Code Online (Sandbox Code Playgroud)

nic*_*ckb 2

首先,您需要确保不会通过缩短字符串来破坏单词。然后,您需要将所有<span class="highlight">标记附加到缩短的字符串的末尾。这是我的想法(大约8行!):

function limit_text($text, $length){
    if( strlen( $text) < $length) {
        return $text;
    }

    // Truncate the string without breaking words
    list( $wrapped) = explode("\n", wordwrap( $text, $length));

    // Get the span of text occurring after the wrapped string
    $remainder = substr( $text, strlen( $wrapped));

    // Add the "to be continued" to $wrapped
    $wrapped .= ' to be continued ... ';

    // Now, grab all of the <span class="highlight"></span> tags in the $remainder
    preg_match_all( '#<span class="highlight">[^<]+</span>#i', $remainder, $matches);

    // Add the <span> tags to the end of the string, separated by a comma, if present
    $wrapped .= implode( ', ', $matches[0]);

    return $wrapped;
}
Run Code Online (Sandbox Code Playgroud)

现在,进行原始测试:

$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the <span class=\"highlight\">author</span> of the question";
$bio = limit_text( $bio,85);
var_dump( htmlentities( $bio));
Run Code Online (Sandbox Code Playgroud)

输出

string(165) "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way to be continued ... <span class="highlight">author</span>"
Run Code Online (Sandbox Code Playgroud)

现在,另一个使用多个<span>标签的测试:

输出

$bio = 'Hello, what about a <span class="highlight">span tag</span> before the limit? Or what if I have many <span class="highlight">span tags</span> <span class="highlight">after</span> <span class="highlight">the</span> limit?';
$bio = limit_text( $bio,85);
var_dump( htmlentities( $bio));

string(308) "Hello, what about a <span class="highlight">span tag</span> before the limit? Or what to be continued ... <span class="highlight">span tags</span>, <span class="highlight">after</span>, <span class="highlight">the</span>" 
Run Code Online (Sandbox Code Playgroud)

如果您有更多测试用例,或者对上面的功能有修改,请告诉我,我可以修复它!