【发布时间】:2021-10-20 02:54:54
【问题描述】:
我正在尝试替换我的自定义短代码,但函数 str_replace 返回空。
我有一个固定的搜索列表。
$search = array(15) {
[0]=>
string(13) "{{user_name}}"
[1]=>
string(17) "{{ticket_number}}"
[2]=>
string(15) "{{ticket_link}}"
[3]=>
string(18) "{{ticket_subject}}"
[4]=>
string(22) "{{ticket_description}}"
}
$replace = array(1) {
[0]=>
string(23) "ticket with attachments"
}
$subject = string(20) "{{ticket_subject}}"
echo str_replace($search, $replace, trim($subject));
为什么结果为空?
【问题讨论】:
-
php 文档说:
If replace has fewer values than search, then an empty string is used for the rest of replacement values.两个数组需要相同的大小和顺序。 -
这是无效的 PHP 代码。如果您想将
$search中的所有元素替换为相同的字符串,请将$replace设为字符串 -
如果我可以用另一种方式解决我的代码示例?
-
both arrays need the same size and order...应该给你足够的信息来解决它。或者查看已经有替代方法的 brombeer 的答案。