The method for inserting a substring in a PHP string is summarized as original. fontcolor_PHP tutorial

Source: Internet
Author: User
The method for inserting a substring in a PHP string is summarized as original, fontcolor. The method for inserting a substring in a PHP string is summarized as original. fontcolor this article describes how to insert a substring in a PHP string. For your reference, see: first, insert the substring into the PHP string to summarize the original method, fontcolor

This example describes how to insert a substring into a PHP string. We will share this with you for your reference. The details are as follows:

First, let's take a look at a common online method:

Method 1: string traversal

Function str_insert ($ str, $ I, $ substr) {for ($ j = 0; $ j <$ I; $ j ++) {$ startstr. = $ str [$ j];} for ($ j = $ I; $ j
 
  

The preceding method uses string traversal and reorganization to insert sub-strings.

Let's take a look at an improvement method provided by the customer's house:

Method 2: Use the substr function for truncation and combination

Function str_insert2 ($ str, $ I, $ substr) {// Method 2: Intercept the substr function $ 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 combined into this sentence} $ str = "1234567890"; $ sstr = "new_word"; echo str_insert2 ($ str, 5, $ sstr); // output: 12345new_word67890

This method uses the substr function to intercept strings and then assemble strings to insert substrings.

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

Method 3: Use the substr_replace function to insert a substring.

Echo substr_replace ($ str, $ sstr, 5, 0); // output directly here: 12345new_word67890

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.