Php string replacement function

Source: Internet
Author: User
This article describes the character replacement functions of two php instances for beginners. one is str_ireplace () and the other is substr_replace (). both functions are easy to use. For more information, see.

This article tells php beginners about the character replacement functions of two php instances, str_ireplace () and substr_replace.

String replacement can be implemented using two common functions: str_ireplace () and substr_replace ().

Str_ireplace () function

Replace the string specified in the original string with the new substring. syntax:

Mixed str_ireplace (mixed search, mixed replace, mixed subject [, int & count])

Search: Required parameter. it specifies the string to be searched.

Replace: a required parameter that specifies the value to replace.

The subject parameter is a required parameter that specifies the search range.

The count parameter is an optional parameter (an optional parameter with brackets). This parameter is used to obtain the number of replicas.

The instance code is as follows:

  1. $ Str2 = "xx ";
  2. $ Str1 = "**";
  3. $ Str = "the address of XX website is www.phpfensi.com. xx website records some notes and thoughts about learning php and various software knowledge ";
  4. Echo str_ireplace ($ str2, $ str1, $ str); // value searched for str2, value replaced by str1, str range
  5. ?>

In this example, we will demonstrate the str_ireplace () function with array and count variables. the code is as follows:

  1. $ Arr = array ("blue", "red", "green", "yellow ");
  2. Print_r (str_ireplace ("red", "pink", $ arr, $ I ));
  3. Echo "Replacements: $ I ";
  4. ?>
  5. // Output:
  6. Array
  7. (
  8. [0] => blue
  9. [1] => pink
  10. [2] => green
  11. [3] => yellow
  12. )

Replacements: 1 example 3,The code is as follows:

  1. $ Find = array ("Hello", "world ");
  2. $ Replace = array ("B ");
  3. $ Arr = array ("Hello", "world ","! ");
  4. Print_r (str_ireplace ($ find, $ replace, $ arr ));
  5. ?>
  6. // Output:
  7. Array
  8. (
  9. [0] => B
  10. [1] =>
  11. [2] =>!
  12. )

Substr_replace () function

Replaces some strings in a specified string. syntax:

String substr_replace (string str, string repl, int start, [int length])

Parameter str: specifies the original string to be operated.

Parameter repl: a required parameter that specifies the new string after replacement.

Parameter start: specifies the position at which the string is replaced.

Parameter length: specify the length of the returned string.

The instance code is as follows:

  1. Substr_replace ('egg', 'X',-1,-1); // eggxs
  2. Substr_replace ('egg', 'X',-1,-2); // eggxs
  3. Substr_replace ('egg', 'X',-1,-2); // eggxs
  4. ?> Same:
  5. Substr_replace ('egg', 'X',-); // eggxs
  6. ?>
  7. Substr_replace ('huevos ', 'X',-2,-2); // huevxos
  8. Substr_replace ('huevos ', 'X',-2,-3); // huevxos
  9. Substr_replace ('huevos ', 'X',-2,-3); // huevxos
  10. ?> Same:
  11. Substr_replace ('huevos ', 'X',-2, 0); // huevxos
  12. ?>

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.