Home  >  Article  >  Backend Development  >  PHP在字符串指定位置插入一段字符串

PHP在字符串指定位置插入一段字符串

WBOY
WBOYOriginal
2016-06-20 13:02:481981browse

php在字符串指定位置插入一段字符串

//插入一段字符串
/*
 * @param string $str    # 待处理字符串
 * @param int    $i      # 插入位置
 * @param string $substr # 需要插入的字符串
 */
function str_insert($str, $i, $substr) 
{ 
	for($j=0; $j<$i; $j++){ 
		$startstr .= $str[$j]; 
	} 
	for ($j=$i; $j<strlen($str); $j++){ 
		$laststr .= $str[$j]; 
	} 
	$str = ($startstr . $substr . $laststr); 
	return $str; 
}

 


Statement:
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