我需要用相同的文本变量替换字符串的所有出现,如果存在php


I need to replace all occurences of a string with the same text variable if exists php

例如:

$str = 'This is stackoverflow and stackexchange.';

如果我们定义了变量$stackoverflow = 'awesome';``$stackexchange = 'great';

$str变为:

'This is awesome and great.'

str_ireplace(array('stackoverflow','stackexchange'),array('awesome','great'),$your_string);

希望这是你想要的

像这样?你可以把所有的名字放在数组中。

<?php
  $old_values = array('stackoverflow','stackexchange');
  $new_values = array('awesome','great');
  $str = 'This is stackoverflow and stackexchange.';
  $str = str_replace($old_values,$new_values,$str);
?>