定义和用法
str_replace() 函数使用一个字符串替换字符串中的另一些字符。
语法
str_replace(find,replace,string,count)
参数 描述
find 必需。规定要查找的值。
replace 必需。规定替换 find 中的值的值。
string 必需。规定被搜索的字符串。
count 可选。一个变量,对替换数进行计数。
提示和注释
注释:该函数对大小写敏感。请使用 str_ireplace() 执行对大小写不敏感的搜索。
注释:该函数是二进制安全的。
例子 1
复制代码 代码如下:
echo str_replace("world","John","Hello world!");
?>
输出:
Hello John!
例子 2
在本例中,我们将演示带有数组和 count 变量的 str_replace() 函数:
复制代码 代码如下:
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "Replacements: $i";
?>
输出:
Array
(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)
Replacements: 1
例子 3
复制代码 代码如下:
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>
输出:
Array
(
[0] => B
[1] =>
[2] => !
)
漏洞相关函数:
$arr1 = Array(
'http://img.jb51.net/img/offer/29/24/70/20/29247020',
'http://img.jb51.net/img/offer/29/24/70/20/29247020-1',
'http://img.jb51.net/img/offer/29/24/70/20/29247020-2'
);
$arr2 = Array(
'http://localhost/root/ups/af48056fc4.jpg',
'http://localhost/root/ups/cf33240aa3.jpg',
'http://localhost/root/ups/c30e40419b.jpg'
);
$data = '



$data = str_replace($arr1,$arr2,$data);
var_dump($data);
?>
替换后的结果是:
string(169) "



解决办法:
function strrplace($arr1,$arr2,$data){
if(is_array($arr1)) {
foreach($arr1 as $key => $value) {
$data = str_replace_once($value, $arr2[$key], $data);
} }
return $data;
}
function str_replace_once($needle, $replace, $data) //替换第一次
{
$pos = strpos($data, $needle);
if ($pos === false) {
return $data;
}
return substr_replace($data, $replace, $pos, strlen($needle));
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)