PHP string replacement function instance parsing

Source: Internet
Author: User
The string replacement technology can be used to replace specified characters in a specified string. The string replacement technique can be implemented using the following two functions: the str_irplace () function and the substr_replace () function. The string replacement technology can be used to replace specified characters in a specified string. The string replacement technique can be implemented using the following two functions: the str_irplace () function and the substr_replace () function.

Str_irplace () function

Replace the specified string in the original string with a new string (substring.

The syntax format is as follows:

str_irplace(search, replace, subject, count)

Note: replace all the parameters that appear in the subject parameter with the replace parameter. the count parameter indicates the number of times the string is executed. The str_irplace () function is case-insensitive.

Parameter Description
Search Required parameter, specifying the string to be searched
Replace Required parameter to specify the value to be replaced
Subject Required parameter to specify the search range
Count Optional. number of times the replacement is executed.

Replace the specified string in the text and output the replaced result. the instance code is as follows:

 

Output result:Watching ** a game is like a single lover who does not love you. Ta gives you a little bit of a face and you think there is a scene.

Note: This function is case-insensitive when performing the replace Operation. if you need to differentiate the case, you can use the str_replace () function.

The most common character string replacement technology is in keyword processing of search engines. you can use string replacement technology to replace the color of the searched character string keywords, such as the red keyword search function, make the search results easier for users to view.

Note: the query keyword is marked with a special color, font size, or font. This allows the viewer to quickly query the required keywords, this allows viewers to search for the desired content from the search results. The keyword red is applicable to fuzzy search.

The following uses a specific example to describe how to achieve the red mark function of the query keyword.

Use the str_irplace () function to replace the query keyword. when the queried information is displayed, replace the font of the output keyword with red. The sample code is as follows:

 

Output result:Watching the national football match is like a single lover who does not love you. Ta gives you a little bit of a good face and you think there is a drama.

Note: The red keyword search function is widely used in search engines. we hope that you can develop more flexible and convenient programs through instance learning.

Substr_replace () function

Replaces some of the specified strings.

The syntax format is as follows:

substr_replace(string, replacement, start, length)

The parameters are described as follows:

Parameter Description
String Specify the original string to be operated
Replacement New string after replacement
Start Start position of string replacement
Length Optional parameter, specifying the length of the string to be replaced

Note: The start parameter is a positive number, indicating that the start position starts from the start of the string; a negative number indicates that the start position starts from the end of the string; and 0 indicates the first character in the start position.

The default value of length is the entire string. A positive number indicates that the starting position starts from the beginning of the string. a negative number indicates that the starting position starts from the end of the string. 0 indicates that the insertion is not an alternative.

Note: If the start parameter is set to a negative value and the length value is smaller than or equal to the start parameter, the length value is automatically 0.

Use the substr_replace () function to replace the specified string, as shown in the following example:

 

Output result:Use today's hard work to return ten times the reward for tomorrow!

Note: In the above code, the substr_replace () function is used to replace the string "double" with the string "ten times ".

The above is the details of PHP replacement string function instance parsing. For more information, see other related articles in the first PHP community!

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.