搜索
首页php教程php手册php substr_replace替换字符串一些实例

php substr_replace替换字符串一些实例

Jun 13, 2016 am 09:51 AM
phpreplacestrsubstr字符串实例替换

substr_replace与str_replace有一点像就是直接把字符串替换一部份了,下面小编来给各位同学介绍一下 操作方法。

substr_replace() 函数把字符串的一部分替换为另一个字符串。

用法

substr_replace(string,replacement,start,length)

注意当字符串包含中文时,不经过特殊处理会出现乱码。

 代码如下 复制代码

$string1="123456789";  
 
echo substr_replace($string1,'###',0);
//###
echo substr_replace($string1,'###',5);
//12345###  
 
echo substr_replace($string1,'###',0,0);
//###123456789
 
echo substr_replace($string1,'###',8,-2);
//12345678###9
 
echo substr_replace($string1,'###',-6,-1);
//123###9
echo "n";
echo substr_replace($string1,'###',-1);
//123###9
echo "n";
echo substr_replace($string1,'###',1,-1);
//1###9
 
echo substr_replace($string1,'###',1,1);
//1###3456789
?>

例2

   

 代码如下 复制代码
 $var = 'ABCDEFGH:/MNRPQR/';
 echo "Original: $var
n";
 /* These two examples replace all of $var with 'bob'. */
 echo substr_replace($var, 'bob', 0) . "
n";
 echo substr_replace($var, 'bob', 0, strlen($var)) . "
n";
 /* Insert 'bob' right at the beginning of $var. */
 echo substr_replace($var, 'bob', 0, 0) . "
n";
 /* These next two replace 'MNRPQR' in $var with 'bob'. */
 echo substr_replace($var, 'bob', 10, -1) . "
n";
 echo substr_replace($var, 'bob', -7, -1) . "
n";
 /* Delete 'MNRPQR' from $var. */
 echo substr_replace($var, '', 10, -1) . "
n";
?>

Original: ABCDEFGH:/MNRPQR/
bob
bob
bobABCDEFGH:/MNRPQR/
ABCDEFGH:/bob/
ABCDEFGH:/bob/
ABCDEFGH://

将过长的字符串用省略号代替一部分

下面的程序可以将过长的字符串保留首尾,中间用省略号代替。

   

 代码如下 复制代码
 $longString = 'abcdefghijklmnopqrstuvwxyz0123456789z.jpg';
 $separator = '...';
 $separatorlength = strlen($separator) ;
    // 需要保留的字符串
 $maxlength = 25 - $separatorlength;
    // 从一半的长度开始
 $start = $maxlength / 2 ;
    // 计算偏移量
 $trunc =  strlen($longString) - $maxlength;
 echo substr_replace($longString, $separator, $start, $trunc);
 //prints "abcdefghij...56789z.jpg"
?>

程序运行结果:

abcdefghijk...456789z.jpg

Program List:将多出的字符用省略号代替

 代码如下 复制代码

 
function truncate($text,$numb)
{
 $text = html_entity_decode($text, ENT_QUOTES);
 if (strlen($text) > $numb)
 {
  $text = substr($text, 0, $numb);
  $text = substr($text,0,strrpos($text," "));
     //This strips the full stop:
     if ((substr($text, -1)) == ".")
  {
         $text = substr($text,0,(strrpos($text,".")));
     }
  $etc = "...";
  $text = $text.$etc;
 }
 $text = htmlentities($text, ENT_QUOTES);
 return $text;
}
//Call function
$text = 'welcome to nowamagic, welcome to nowamagic, welcome to nowamagic';
$result = truncate($text, 35);
echo $result;
?>

好了你大概会知道此函数的作用了。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中