substr_replace与str_replace有一点像就是直接把字符串替换一部份了,下面小编来给各位同学介绍一下 操作方法。
substr_replace() 函数把字符串的一部分替换为另一个字符串。
用法
substr_replace(string,replacement,start,length)
注意当字符串包含中文时,不经过特殊处理会出现乱码。
代码如下 | 复制代码 |
$string1="123456789"; |
例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:将多出的字符用省略号代替
代码如下 | 复制代码 |
|
好了你大概会知道此函数的作用了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
