PHP:str_replace 问题
我注意到 str_replace 函数的奇怪行为。 您能告诉我为什么我得到的是 no_4 而不是 no_2 中的数字 3 吗?
情况如下:
$pattern = array(1,2,3);
$change = array(1,3,4);
$sql = "SELECT * FROM %s WHERE no_1 IN (%s) AND no_2 IN (%s) AND no_3 IN (%s)";
$test_multiply[] = str_replace($pattern, $change, $sql);
哪个给出:
Array ( [0] => SELECT * FROM %s WHERE no_1 IN (%s) AND no_4 IN (%s) AND no_4 IN (%s) )
你能告诉我我应该做什么才能收到 no_3 而不是 no_2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
str_replace()
的文档说 < em>(引用):我相信您正是处于这种情况:
no_2
被替换为no_3
- 因为您的$pattern 中的 第二 项
和$change
数组no_4
在您的$pattern
和$change
数组中添加 项To avoid that specific situation, you might try reversing the order of the items in those two arrays :
,您会得到以下结果:
The documentation for
str_replace()
says (quoting) :I believe you are precisely in this situation :
no_2
gets replaced tono_3
-- because of the second item in your$pattern
and$change
arraysno_3
gets replaced tono_4
-- because of the thid item in your$pattern
and$change
arraysTo avoid that specific situation, you might try reversing the order of the items in those two arrays :
And you'd get the following result :
如果您将
str_replace
与一组针一起使用,则每个针都会被替换为单独的 <代码>str_replace调用。您可以想象类似的事情在内部发生:因此在第一次迭代中,所有
1
都被1
替换,然后所有2
都被 < code>3,然后将所有3
替换为4
:SELECT * FROM %s WHERE no_1 IN (%s) AND no_2 IN ( %s) AND no_3 IN (%s)
从 %s 中选择 *,其中 no_1 IN (%s) AND no_3 IN (%s) AND no_3 IN (%s)
SELECT * FROM %s WHERE no_1 IN (%s) AND no_4 IN (%s) AND no_4 IN (%s)
要进行同时替换,您可以使用
preg_replace_callback
并为每个匹配的模式调用映射函数:我使用 匿名函数,但它也适用于
create_function
。这样,替换的顺序就不再重要了。您甚至可以交换两个值:
If you use
str_replace
with an array of needles, every needle is replaced in a separatestr_replace
call. You can imagine that something like this happens internally:So in the first iteration all
1
are replaced by1
, then all2
are replaced by3
, and then all3
is replaced by4
:SELECT * FROM %s WHERE no_1 IN (%s) AND no_2 IN (%s) AND no_3 IN (%s)
SELECT * FROM %s WHERE no_1 IN (%s) AND no_3 IN (%s) AND no_3 IN (%s)
SELECT * FROM %s WHERE no_1 IN (%s) AND no_4 IN (%s) AND no_4 IN (%s)
To do a simultaneous replacement, you can use
preg_replace_callback
and call a mapping function for each matched pattern:I used anonymous function in this example but it will also work with
create_function
.With this the order of the replacement doesn’t matter. You can even exchange two values:
首先,您将 1 替换为 1,这样就可以了
SELECT * FROM %s WHERE no_1 IN (%s) AND no_2 IN (%s) AND no_3 IN (%s)
然后,替换 2与 3 一起,得到
SELECT * FROM %s WHERE no_1 IN (%s) AND no_3 IN (%s) AND no_3 IN (%s)
然后将 3 替换为 4,结果是
SELECT * FROM %s WHERE no_1 IN (%s) AND no_4 IN (%s) AND no_4 IN (%s)
也许您应该将 2 替换为另一个值(而不是 3),您可以将其替换稍后再来。
First, you're replacing 1 with 1, so thats fine
SELECT * FROM %s WHERE no_1 IN (%s) AND no_2 IN (%s) AND no_3 IN (%s)
Then, replacing 2 with 3, to get
SELECT * FROM %s WHERE no_1 IN (%s) AND no_3 IN (%s) AND no_3 IN (%s)
Then replacing 3 with 4, resulting in
SELECT * FROM %s WHERE no_1 IN (%s) AND no_4 IN (%s) AND no_4 IN (%s)
Maybe you should replace 2 with another value (not 3), which you can replace again later.
因为一旦你将 2 替换为 3,下一次替换将始终将 3 替换为 4
Because once you replace 2 with 3 the next replace will always replace 3 with 4