Home > Backend Development > PHP Tutorial > How to insert substring into string in PHP

How to insert substring into string in PHP

不言
Release: 2023-03-31 13:24:01
Original
6431 people have browsed it

This article mainly introduces the method of inserting substrings into PHP strings, and compares and analyzes three different implementation methods of string traversal, substr method interception and combination, and direct use of the substr_replace function to insert substrings. For common tips related to PHP string operations, friends in need can refer to the following

The example in this article describes the method of inserting substrings into PHP strings. Share it with everyone for your reference, the details are as follows:

First, let’s take a look at a common method on the Internet:

Method 1: String traversal

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;
}
$str="1234567890";
$sstr="new_word";
echo str_insert($str,5,$sstr);//输出:12345new_word67890
Copy after login

The above method uses string traversal and reorganization to implement the substring insertion function.

Let’s take a look at an improvement method given by Script House:

Method 2: Use the substr function to intercept and combine

function str_insert2($str,$i,$substr){//方法二:substr函数进行截取
  $start=substr($str,0,$i);
  $end=substr($str,$i);
  $str = ($start . $substr . $end);
  return $str;
  //return substr($str,0,$i).$substr.substr($str,$i);//上述代码可综合成这一句
}
$str="1234567890";
$sstr="new_word";
echo str_insert2($str,5,$sstr);//输出:12345new_word67890
Copy after login

This method directly uses the substr function to intercept the string, and then assemble the string to achieve the substring insertion effect.

Finally, Script House provides you with the most direct method:

Method 3: Directly use the substr_replace function to insert substrings

echo substr_replace($str,$sstr,5,0);
//此处直接输出:12345new_word67890
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About thinkPHP’s built-in string interception function method

PHP implements querying the differences between two arrays Element methods

The above is the detailed content of How to insert substring into string in PHP. For more information, please follow other related articles on the PHP Chinese website!

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