PHP str_replace function arguments are used as arrays

Source: Internet
Author: User

Str_replace for string substitution

The code is as follows Copy Code

<?php
Str_replace ("PHP", "java", "PHP Tutorial 4");
Java Tutorial 4
?>

The variable being searched is a string

The code is as follows Copy Code

<?php
/*
The variable being searched is a string
*/
$array =array ();
$array []= ' PHP tutorial 1];
$array []= ' PHP tutorial 2];
$array []= ' PHP tutorial 3];
$array []= ' PHP tutorial 4];
$arrayNew =str_replace ("PHP", "Java", $array);

Print_r ($ARRAYNEW);
/*
Array
(

[0] => Java Tutorial 1

[1] => Java Tutorial 2

[2] => Java Tutorial 3

[3] => Java Tutorial 4
)
*/
?>

You can also handle variables in the form of an array, and the value you look for and the value you replace can be a pair of relationships

The code is as follows Copy Code

<?php
/*
The variable to look for is an array form
*/
$array =array ();
$array []= ' PHP tutorial 1];
$array []= ' PHP tutorial 2];
$array []= ' PHP tutorial 3];
$array []= ' PHP tutorial 4];
$find =array ("php", "tutorial");
$replace =array ("Java", "case");
$arrayNew =str_replace ($find, $replace, $array);
Print_r ($ARRAYNEW);
/*
Array
(

[0] => Java Case 1

[1] => Java case 2

[2] => Java case 3

[3] => Java case 4
)
*/
?>

The lookup value and the replacement value can be a many-to-many relationship

  code is as follows copy code

<?php
/*
The variable to look for is an array form
*/
$array =array ();
$array []= ' PHP tutorial 1];
$array []= ' PHP tutorial 2];
$array []= ' PHP tutorial 3];
$array []= ' PHP tutorial 4];
$find =array ("php", "tutorial");
$arrayNew =str_replace ($find, "Java", $array);
 
Print_r ($ARRAYNEW);
 
/*
Array
(
   
[0] => javajava1
   
[1] =& Gt JAVAJAVA2
   
[2] => javajava3
   
[3] => javajava4
)
*/

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.