Home >php教程 >php手册 >php替换字符串中间字符为省略号的方法

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

WBOY
WBOYOriginal
2016-06-06 20:04:341371browse

这篇文章主要介绍了php替换字符串中间字符为省略号的方法,可实现将字符串中间部分替换成省略号的功能,适用于账号、手机号等敏感信息的部分隐藏,需要的朋友可以参

本文实例讲述了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程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn