php - PHP替换字符串的一部分并将这些替换的部分作为单独的字符串
问题描述
我有一个像这样的字符串,"[one]sdasf[two]sad[three]"
因此,我想将 [one] 替换为"replaced_one"
,将 [two] 替换为"replaced_two"
。我需要那些替换的值作为单独的字符串。
$rep_one = "replaced_one";
$rep_two = "replaced_two";
解决方案
这是一个简单的 PHP 函数str_replace()
(有关更多信息,请转到PHP 手册)
该函数需要三个参数:
str_replace(
# Search - string section to be replaced
# Replace - the content which will replace the `Search`
# Subject - The string you want to alter
)
所以在你的情况下,你可以简单地这样做:
$str = "[one]sdasf[two]sad[three]";
$str = str_replace('[one]', 'replace_one', $str);
$str = str_replace('[two]', 'replace_two', $str);
echo $str; // replace_onesdasfreplace_twosad[three]
推荐阅读
- c++ - c ++线程未产生导致互斥锁未解锁
- typescript - 如何避免 TypeScript 中的动态 keyof 对象分配错误
- c# - 是否可以从 ASP.NET Core 3 控制台应用程序启动本地 Vue CLI 3 Web 应用程序?
- laravel - 在 Laravel/Vue SPA 中实现 Service Worker 的最佳方式
- javascript - 当链接未硬编码时,将给定链接转换为 PDF 不起作用
- oauth - oauth 是否会在每个请求中发布一个新令牌?
- tortoisesvn - 如何重置 TotroiseSvn 信息?
- c# - 将网格视图列值插入文本框并在按钮控件上下拉
- angular - 角度路由,加载完整(子)项目
- firebase - Flutter 无法从 Google API 获取数据