Home > php教程 > php手册 > PHP substr_replace()函数的几个应用例子

PHP substr_replace()函数的几个应用例子

WBOY
Release: 2016-06-13 09:39:02
Original
1517 people have browsed it

substr_replace()函数介绍

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

语法:substr_replace(string,replacement,start,length)

  • 参数string,必需。规定要检查的字符串。
  • 参数replacement,必需。规定要插入的字符串。
  • 参数start,必需。规定在字符串的何处开始替换。正数 - 在第 start 个偏移量开始替换;负数 - 在从字符串结尾的第 start 个偏移量开始替换;0 - 在字符串中的第一个字符处开始替换。
  • 参数charlist,可选。规定要替换多少个字符。正数 - 被替换的字符串长度;负数 - 从字符串末端开始的被替换字符数;0 - 插入而非替换。

如果 start 是负数且 length 小于等于 start,则 length 为 0。

Program List:substr_replace()函数基本用法

    
<?php
	$var = 'ABCDEFGH:/MNRPQR/';
	echo "Original: $var<hr />\n";
	/* These two examples replace all of $var with 'bob'. */
	echo substr_replace($var, 'bob', 0) . "<br />\n";
	echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />\n";
	/* Insert 'bob' right at the beginning of $var. */
	echo substr_replace($var, 'bob', 0, 0) . "<br />\n";
	/* These next two replace 'MNRPQR' in $var with 'bob'. */
	echo substr_replace($var, 'bob', 10, -1) . "<br />\n";
	echo substr_replace($var, 'bob', -7, -1) . "<br />\n";
	/* Delete 'MNRPQR' from $var. */
	echo substr_replace($var, '', 10, -1) . "<br />\n";
?>
Copy after login

程序运行结果:

Original: ABCDEFGH:/MNRPQR/
bob
bob
bobABCDEFGH:/MNRPQR/
ABCDEFGH:/bob/
ABCDEFGH:/bob/
ABCDEFGH://
Copy after login

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

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

    
<?php
	$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"
?>
Copy after login

程序运行结果:

abcdefghijk...456789z.jpg
Copy after login

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

  
<?php
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 bkjia, welcome to bkjia, welcome to bkjia';
$result = truncate($text, 35);
echo $result;
?>
Copy after login

程序运行结果:

welcome to bkjia, welcome to...
Copy after login

Program List:给金钱数额加上逗号

  
<?php 
	$price = "163000"; 
	$price = substr_replace($price, ',', -3, 0); 
	echo $price;
?>
Copy after login

程序运行结果:

163,000
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template