假设我们有这个文本
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.1/css/bulma.min.css">
<div class="container">
<div class="columns is-desktop">
<div class="column is-8-desktop">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Eligendi nostrum voluptatum porro maiores animi dignissimos amet consectetur cumque sapiente eos, explicabo, accusamus modi mollitia labore corporis inventore eveniet esse quam?
</div>
<div class="column">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Iusto alias necessitatibus doloremque magni minima nobis facere, quibusdam autem eveniet voluptatum amet explicabo voluptates, laudantium praesentium? Ab repellat eius doloremque labore.
</div>
</div>
</div>
我想用不同的值替换所有出现的{abc},所以在最后的$ str中它应该是例如:
$str = "Text {abc} text text text {abc}";
答案 0 :(得分:0)
我为你写了自定义函数:)
function myReplaceFunc($needed, $str, $values)
{
$strArray = explode($needed, $str);
$replacedStr = '';
$i = 0;
foreach($strArray as $key => $str){
$replacedStr = $str != '' ? $replacedStr . $str . $values[$i] : $replacedStr;
$i = $str != '' ? $i + 1 : $i;
}
return $replacedStr;
}
echo myReplaceFunc('{abc}', 'Text {abc} text text text {abc}', ['DOG', 'CAT'])