找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 496|回复: 0
打印 上一主题 下一主题

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

[复制链接]

2500

主题

2513

帖子

7520

积分

论坛元老

Rank: 8Rank: 8

积分
7520
跳转到指定楼层
楼主
发表于 2018-2-14 05:46:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了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 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
演示范例:
// 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)
希望本文所述对大家的php程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • PHP 字符串分割和比较
  • php下关于中英数字混排的字符串分割问题
  • PHP 字符串正则替换函数preg_replace使用说明
  • php 字符串替换的方法
  • php字符串分割函数explode的实例代码
  • PHP preg_replace() 正则替换所有符合条件的字符串
  • php中利用explode函数分割字符串到数组
  • php+正则将字符串中的字母数字和中文分割
  • PHP中strtr字符串替换用法详解
  • php字符串过滤与替换小结
  • php支持中文字符串分割的函数
  • php使用自定义函数实现汉字分割替换功能示例
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端