Insert substring method in PHP string summary original, FontColor
This example describes the insertion of a substring method in a PHP string. Share to everyone for your reference, as follows:
First look at a common online approach:
Method One: String traversal
function Str_insert ($str, $i, $substr) {for ($j =0; $j < $i; $j + +) {$startstr. = $str [$j]; } for ($j = $i; $j
The above method uses the string traversal reorganization to implement the substring insertion function.
Let's take a look at some of the improvements offered by the Help house:
Method Two: Using SUBSTR function to intercept and combine
function Str_insert2 ($str, $i, $substr) {//Method two: substr function to intercept $start =substr ($str, 0, $i); $end =substr ($str, $i); $str = ($start. $substr. $end); return $str; Return substr ($str, 0, $i). $substr. substr ($str, $i);//The above code can be synthesized into this sentence} $str = "1234567890"; $sstr = "New_word"; Echo str_ Insert2 ($str, 5, $SSTR);//output: 12345new_word67890
The method uses the SUBSTR function to intercept the string, and then assemble the string to achieve the insertion effect of the substring.
Finally help the home to provide you with a most direct way:
Method Three: Inserting substrings directly using the Substr_replace function
Echo Substr_replace ($str, $sstr, 5,0);//Direct output here: 12345new_word67890
More about PHP related content readers can view the topic: "PHP array" operation Skills Daquan, "PHP Data structure and algorithm tutorial", "PHP Math Skills Summary", "PHP date and Time usage summary", "PHP object-oriented Programming introduction Tutorial", " PHP String Usage Summary, "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"
I hope this article is helpful to you in PHP programming.
http://www.bkjia.com/PHPjc/1123799.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123799.html techarticle Insert substring method in PHP string summarize original, FontColor This example describes the insertion of a substring method in a PHP string. Share to everyone for your reference, as follows: First ...