我想用数据字段数组(值864和987)的实际值替换下面给出的字段,但是我不知道如何[crowdpdf-total=field_585b0b3288d2e]
用例如864
和[crowdpdf-total=field_585b0b3288d2f]
替换987
。
数组:
array (
'field_585b0b3288d2e'=> 864,
'field_585b0b3288d2f => 987
)
字符串:
etc etc etc content
[crowdpdf-total=field_585b0b3288d2e]
etc etc [crowdpdf-total=field_585b0b3288d2f]
etc content
我擅长很多事情,但是由于某些原因,我从未完全了解过regex ...我猜想regex是[crowdpdf-total={nameoffield}]
用实际值代替的方法吗?正则表达式是去这里的方式吗?
我想要的字符串是:
etc etc etc content
864
etc etc 987
etc content
澄清:
实际值我知道如何替换,因此我可以像替换那样,[crowdpdf-total=864]
并且可以[crowdpdf-total=
通过仅替换[crowdpdf-total=
为空字符串来删除它,但是然后在每个值的末尾都有一个多余的括号。
您不需要为此使用正则表达式。一个简单的字符串替换即可:
<?php
$string = <<<EOT
etc etc etc content
[crowdpdf-total=field_585b0b3288d2e]
etc etc [crowdpdf-total=field_585b0b3288d2f]
etc content
EOT;
$replacements = [
'field_585b0b3288d2e' => 864,
'field_585b0b3288d2f' => 987,
];
foreach ($replacements as $find => $replace) {
$string = str_replace("[crowdpdf-total=$find]", $replace, $string);
}
echo $string;
它输出:
etc etc etc content
864
etc etc 987
etc content
如果需要,可以在线进行测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句