Several application examples of PHP substr_replace() function_PHP tutorial

WBOY
Release: 2016-07-13 10:33:53
Original
1572 people have browsed it

Introduction to substr_replace() function

The substr_replace() function replaces part of a string with another string.

Syntax: substr_replace(string,replacement,start,length)

  • Parameter string, required. Specifies the string to check.
  • Parameter replacement, required. Specifies the string to be inserted.
  • Parameter start, required. Specifies where in the string to begin replacement. Positive number - start replacement at start offset; negative number - start replacement at start offset from the end of the string; 0 - start replacement at the first character in the string.
  • Parameter charlist, optional. Specifies how many characters to replace. Positive number - the length of the string to be replaced; negative number - the number of characters to be replaced from the end of the string; 0 - insertion instead of replacement.

If start is negative and length is less than or equal to start, then length is 0.

Program List: Basic usage of substr_replace() function

    
<?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

Program execution result:

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

Program List: Replace part of an overly long string with ellipses

The following program can keep the beginning and end of an overly long string and replace it with ellipses in the middle.

    
<?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

Program execution result:

abcdefghijk...456789z.jpg
Copy after login

Program List: Replace extra characters with ellipsis

  
<?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

Program execution result:

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

Program List: Add comma to money amount

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

Program execution result:

163,000
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752379.htmlTechArticlesubstr_replace() function introduction The substr_replace() function replaces part of a string with another string. Syntax: substr_replace(string,replacement,start,length) parameter string, must...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template