Home > php教程 > php手册 > php字符串插入另一字符串任意位置

php字符串插入另一字符串任意位置

WBOY
Release: 2016-05-23 08:34:01
Original
1583 people have browsed it

下面整理了两个可以在一个字符串中任意位置插入一段字符串了,有两个函数一个是在指定位置,一个是在任意位置了都非常的不错,希望对各位会带来帮助,原理也非常的简单我们只要指定位置然后遍历字符再判断位置然后把字符分开再把新字符push 进入再组成新的字符即可.

例子,代码如下:

$str='字符串'; 
$AllNum = mb_strlen($str,&#39;gbk&#39;).&#39;<br>&#39;; 
echo $AllNum; 
$array=array(); 
$i=0; 
$num=10; 
for ($i;$i<$AllNum/66;$i++){ 
 
   $start=66*$i; 
 
   $array[$i] = mb_substr($str,$start,66,&#39;GB2312&#39;).&#39;-----&#39;;//每66个字后面添加一个&#39;--------&#39; 
} 
foreach ($array as $a) 
{ 
   echo $a; 
}
Copy after login

例子2,代码如下:

<?php 
//插入一段字符串 
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; 
}
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