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
)
*/ |