简体   繁体   English

将字符串值替换为字符串中的另一个值

[英]Replace array values by another one in a string

i try to replace array values by another one in a string, i can't find any PHP function could do this. 我尝试用字符串中的另一个替换数组值,我找不到任何PHP函数可以做到这一点。

this is my code : 这是我的代码:

array_replace(array('id','quote_id'), array('new field 1','new field 2'), $string)

this is my string 这是我的字符串

SELECT id,quote_id,total FROM quotes
$ar = array('id','quote_id');
$replace = array('new field 1','new field 2');

$str = 'SELECT id, quote_id,total FROM quotes';

for($i=0;$i<count($ar);$i++) {
    $str = preg_replace("/\b$ar[$i]\b/u", $replace[$i], $str);
}

echo $str;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM