Pri*_*ner 16
<?php
$a = array('cheese','milk');
$b = array('old cow','udder');
$str = 'I like cheese and milk';
echo str_replace($a,$b,$str); // echos "I like old cow and udder"
Run Code Online (Sandbox Code Playgroud)
或者,如果您不喜欢这样(担心错过匹配的数组值),那么您可以这样做:
$start = $replace = $array();
$str = "I like old cow and udder"
$start[] = 'cheese';
$replace[] = 'old cow';
$start[] = 'milk';
$replace[] = 'udder';
echo str_replace($start,$replace,$str); // echos "I like old cow and udder"
Run Code Online (Sandbox Code Playgroud)
编辑:我看到dnl编辑了这个问题并强调了str_replace太慢的事实.在我对这个问题的解释中,这是因为用户不知道他们会在str_replace中使用数组.