PHP str_replace () function example tutorial

Source: Internet
Author: User

PHP str_replace () function example tutorial

Definition and usage
This str_replace () function replaces some characters with other character strings.

This function works in the following rules:

If the string is searched as an array, it returns an array
If the string is searched for as an array, the search and replacement use each array element.
If you find and replace the array, and replace a small part of it than finding an empty string, it will be used as a replacement
If an array is found and replaced by a string, replacement of the string will be used for every search for value.
Syntax

str_replace(find,replace,string,count)
 
Parameter Description
Find Required. Specified value discovery
Replace Required. Specified value to replace value discovery
String Required. The specified string is searched
Count Optional. Replace the count of a Variable

Tips and instructions
Note: This function is case sensitive. Use str_ireplace () to perform a case-insensitive search.

Note: This function is binary secure.


--------------------------------------------------------------------------------

Example 1

<?phpecho str_replace("world","Peter","Hello world!");?>
 
Output result.
 
Hello Peter!
 

Example 2
In this example, we will show the same array of str_replace () and a counting variable:

 

<?php$arr = array("blue","red","green","yellow");print_r(str_replace("red","pink",$arr,$i));echo "Replacements: $i";?>

Output result

 

Array([0] => blue[1] => pink[2] => green[3] => yellow)Replacements: 1

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.