龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php替换掉字符串中间的字符为省略号

时间:2014-06-26 02:05来源:网络整理 作者:网络 点击:
分享到:
php替换掉字符串中间的字符为省略号 ```{php}/** * Reduce a string by the middle, keeps whole words together * * @param string $string * @param int $max (default 50) * @param string $replacement (default [...]) * @return
/**
 * Reduce a string by the middle, keeps whole words together
 *
 * @param string $string
 * @param int $max (default 50)
 * @param string $replacement (default [...])
 * @return string
 * @author david at ethinkn dot com
 * @author loic at xhtml dot ne
 * @author arne dot hartherz at gmx dot net
 */

function strMiddleReduceWordSensitive($string, $max = 50, $rep = '[...]') {
   $strlen = strlen($string);

   if ($strlen <= $max)
       return $string;

   $lengthtokeep = $max - strlen($rep);
   $start = 0;
   $end = 0;

   if (($lengthtokeep % 2) == 0) {
       $start = $lengthtokeep / 2;
       $end = $start;
   } else {
       $start = intval($lengthtokeep / 2);
       $end = $start + 1;
   }

   $i = $start;
   $tmp_string = $string;
   while ($i < $strlen) {
       if (isset($tmp_string[$i]) and $tmp_string[$i] == ' ') {
           $tmp_string = substr($tmp_string, 0, $i) . $rep;
           $return = $tmp_string;
       }
       $i++;
   }

   $i = $end;
   $tmp_string = strrev ($string);
   while ($i < $strlen) {
       if (isset($tmp_string[$i]) and $tmp_string[$i] == ' ') {
           $tmp_string = substr($tmp_string, 0, $i);
           $return .= strrev ($tmp_string);
       }
       $i++;
   }
   return $return;
   return substr($string, 0, $start) . $rep . substr($string, - $end);
}

//演示范例:
// example:

$text = 'This is a very long test sentence, bla foo bar nothing';

print strMiddleReduceWordSensitive ($text, 30) . "\n";
// Returns: This is a very[...]foo bar nothing (~ 30 chrs)

print strMiddleReduceWordSensitive ($text, 30, '...') . "\n";
// Returns: This is a very...foo bar nothing (~ 30 chrs)
精彩图集

赞助商链接