php - 使用 str_replace 时出现多个数组错误
问题描述
我想替换如下文本。
$text = '<!DOCTYPE html>
<html>
<head>
</head>
<body>
<pre><code>[[EXAMPLE]] this is a text. <br />[[EXAMPLE2]]</code></pre>
<p style="text-align: center;"><br /><br /></p>
</body>
</html>';
$products['type'] = 1;
$products['quantity'] = 2300.0;
$products['grade'] = null;
$searchVal = array(
'[[EXAMPLE]]'
'[[EXAMPLE2]]'
);
$replaceVal = array(
'replace word',
$products
);
$text = str_replace($searchVal, $replaceVal, $text);
我没有运行这段代码。这个失败是“数组到字符串的转换”
我怎样才能做到这一点。
解决方案
根据文档:
如果搜索和替换是数组,则 str_replace() 从每个数组中获取一个值并使用它们来搜索和替换主题
确保两者$searchVal
都是$replaceVal
线性阵列,目前它们不是。
您的$searchVal
定义缺少逗号。
你$replaceVal
是一个数组,其中第二个元素是另一个数组。
推荐阅读
- ssl - 使用 Nginx 的 SSL 直通
- html - 内容脚本匹配
- r - 从列中查找全年每一天的最大数字,并在 R 中创建一个直到该数字的图
- macos-sierra - Mac OS Sierra 日志显示缺少 SSH 源 IP
- php - OVH的PHPmailer smtp错误,设置正确并在邮件应用程序中工作
- prestashop-1.6 - PRESTASHOP 无法访问承运人、库存和仓库
- sql-server - VS 2017 数据库项目“未解决的对象引用”错误
- react-native - React-Native Fetch/Axios 没有 statusText
- android - 如何将cordova平台android@6.2.3更新为android@8.0.0?
- reactjs - Web 应用程序中的 Reactjs 服务工作者