PHP string substitution function _php Tutorial

Source: Internet
Author: User
This article to PHP beginners to talk about two examples of PHP character substitution function, one is str_ireplace () one is substr_replace () These two functions are more useful, there is a need for reference.

String substitution techniques can be implemented using the following two common functions: the Str_ireplace () function and the Substr_replace () function
Str_ireplace () function
Replaces the string that is specified in the original string with the new substring, with the syntax:
Mixed str_ireplace (mixed search,mixed replace,mixed Subject[,int&count])
Parameter search: The necessary parameters, specifying the string to find.
Parameter replace: The required parameter, specifying the value to replace.
Parameter subject: Required parameter, specifies the scope of the lookup.
Parameter count: Optional parameter, (optional parameter with brackets), gets the number of execution substitutions.

Instance:

The code is as follows Copy Code
$str 2 = "xxx";
$STR 1 = "* *";
$str = "The address of a site is www.hzhuti.com, such as a website mainly record some learning PHP notes and impressions and a variety of software knowledge";
Echo Str_ireplace ($str 2, $str 1, $STR); STR2 Lookup value, str1 replacement value, str range
?>

In this example, we will demonstrate the str_ireplace () function with an array and a count variable:

The code is as follows Copy Code

$arr = Array ("Blue", "Red", "green", "yellow");
Print_r (Str_ireplace ("Red", "pink", $arr, $i));
echo "Replacements: $i";
?>

Output:

Array
(
[0] = Blue
[1] = Pink
[2] = Green
[3] = Yellow
)

replacements:1 Example 3

The code is as follows Copy Code

$find = Array ("Hello", "World");
$replace = Array ("B");
$arr = Array ("Hello", "World", "!");
Print_r (Str_ireplace ($find, $replace, $arr));
?> output:

Array
(
[0] = B
[1] = =
[2] = =!
)


Substr_replace () function
Replaces the partial string in the specified string with the syntax:
String Substr_replace (String str,string repl,int start,[int length])
Parameter str: Specifies the original string to manipulate.
Parameter REPL: The necessary parameter that specifies the new string after replacement.
Parameter start: Specifies where the replacement string begins.
Argument Length: Specifies the length of the string returned.
Instance:

copy code

"!--? php
substr_replace (' Eggs ', ' x ', -1,-1); EGGXS
substr_replace (' Eggs ', ' x ', -1,-2);//eggxs
Substr_replace (' Eggs ', ' x ', -1,-2);//eggxs
?> Same as :
!--? php
substr_replace (' Eggs ', ' x ', -1,0);//eggxs
.

!--? php
substr_replac E (' Huevos ', ' x ', -2,-2); Huevxos
Substr_replace (' Huevos ', ' x ', -2,-3);//huevxos
Substr_replace (' Huevos ', ' x ', -2,-3);//huevxos
? > Same as:

!--? php
substr_replace (' Huevos ', ' x ', -2,0);//huevxos
?

For more detailed information, please see: http://www.bKjia.c0m/phper/21/32954.htm

http://www.bkjia.com/PHPjc/631687.html www.bkjia.com true http://www.bkjia.com/PHPjc/631687.html techarticle This article to PHP beginners to talk about two examples of PHP character substitution function, one is str_ireplace () one is substr_replace () These two functions are more useful, there is a need for reference. Characters ...

  • 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.