dsjbest2015 2012-06-20 23:09
浏览 33
已采纳

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

Maybe you guys can help:

I have a variable called $bio with bio data.

$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";

I search the $bio using a set of functions to search for a certain word, lets say "author" which adds a span class around that word, and I get:

$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";

I use a function to limit the text to 85 chars:

$bio = limit_text($bio,85);

The problem is when there are more then 80 chars before the word "author" in $bio. When the limit_text() is applied, I won't see the highlighted word author.

What I need is for the limit_text() function to work as normal, adding all the words that contain the span class highlight at the end. Something like this:

*"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 "*

Hope you understood what I mean, if not, please comment and I'll try to explain better.

Here is my limit_text() function:

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

UPDATE:

$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);

The customHighlights function which adds the span class highlighted:

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;
    }
  • 写回答

3条回答 默认 最新

  • douxuan0698 2012-06-28 01:29
    关注

    First, you need to make sure you don't break words apart by shortening the string. Then you need to append all of the <span class="highlight"> tokens to the end of the shortened string. Here is what I came up with (in about 8 lines!):

    function limit_text($text, $length){
        if( strlen( $text) < $length) {
            return $text;
        }
    
        // Truncate the string without breaking words
        list( $wrapped) = explode("
    ", 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;
    }
    

    Now, with your original test:

    $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));
    

    This outputs:

    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>"
    

    Now, another test with multiple <span> tags:

    This outputs:

    $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>" 
    

    If you have more test cases, or have a modification to the function above, let me know and I can fix it!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题