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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?