时间:2021-07-01 10:21:17 帮助过:23人阅读
substr_replace() 函数把字符串的一部分替换为另一个字符串。
语法:substr_replace(string,replacement,start,length)
如果 start 是负数且 length 小于等于 start,则 length 为 0。
\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://
下面的程序可以将过长的字符串保留首尾,中间用省略号代替。
程序运行结果:
abcdefghijk...456789z.jpg
$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 bkjia, welcome to bkjia, welcome to bkjia'; $result = truncate($text, 35); echo $result; ?>
程序运行结果:
welcome to bkjia, welcome to...
程序运行结果:
163,000
http://www.bkjia.com/PHPjc/752379.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752379.htmlTechArticlesubstr_replace()函数介绍 substr_replace() 函数把字符串的一部分替换为另一个字符串。 语法:substr_replace(string,replacement,start,length) 参数string,必...