PHP replaces some characters in a string (case-sensitive) function str_replace ()

Source: Internet
Author: User

Instance

Replace the character "world" in the string "Hello world!" with "Peter":

<?phpecho Str_replace ("World", "Peter", "Hello world!");? >

Definition and usage

The Str_replace () function replaces some characters in a string (case-sensitive).

The function must adhere to the following rules:

If the searched string is an array, it returns an array.

If the searched string is an array, it will find and replace each element in the group.

If you need to find and replace an array at the same time, and you need to execute the replacement element less than the number of elements found, the extra element is replaced with an empty string.

If you are looking for an array, but only one string is replaced, the substitution string will work for all found values.

Note: The function is case-sensitive. Use the Str_ireplace () function to perform a case-insensitive search.

Note: This function is binary safe.

Grammar

Str_replace (Find,replace,string,count)
parameters description
find required. Specifies the value to find
replace required. Specifies the value that replaces the value in Find.
string required. Specifies the string to be searched.
count optional. A variable that counts the number of replacements.

technical details

return value: Returns a string or array with a replacement value.
php version: 4+
update log: in PHP 5.0, the Count parameter is added.

Before PHP 4.3.3, the function's find and replace parameters will be in an array of problems, causing an empty find index to be ignored when the internal pointer is not replaced on the replace array. The new version does not have this problem.

Since PHP 4.0.5, most parameters can be an array.

More examples

Example 1

Use the Str_replace () function with an array and a count variable:

<?php$arr = Array ("Blue", "Red", "green", "yellow");p Rint_r (Str_replace ("Red", "pink", $arr, $i)); echo "replacements : $i ";? >

Example 2

Use the Str_replace () function with fewer elements to replace than the found element:

<?php$find = Array ("Hello", "World"), $replace = Array ("B"), $arr = Array ("Hello", "World", "!"); Print_r (Str_replace ($find, $replace, $arr));? >

This function converts the string str into the haystack string, replacing all needle with Str. Mlevine@adtraq.com (11-apr-1999) points out that in PHP 3.0.7 version, this function has some bugs, while nadeem@bleh.org (05-jun-1999) supplements in PHP 3.0.8 The function is back to normal.
Usage examples
The following example replaces%body% with black

< php $bodytag = str_replace ("%body%", "Black", "<body text=%body%>"); Echo $bodytag; >

Format:

[@str_replace ("Old content to replace", "new character to replace original content", variable name of the replaced content)] [@str_replace (' Old 1 ', ' Old 2 ', ' Old 3 '), Array (' New 1 ', ' New 2 ', ' new 3 '), $ replaced by the variable name of the content)] [@str_replace (Array (' Old 1 ', ' Old 2 ', ' Old 3 '), ' new content ', ' variable name of the replaced content ')]

Instance:
Many-to-one substitution: Want to remove all <p></p> tags from the content field and replace them with empty

[@str_replace (Array (' <p> ', ' </p> '), ', $Content)]

One-to-one replacement: Want to change all <br> tags in the Content field to <p>

[@str_replace (' <br> ', ' <p> ', $Content)]

Many-to-many replacement: Want to change the Content field <br> <br/>, while <p> for

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.