我需要PHP中的函数才能更换这样的句柄。
$pattern = ':foo/anotherString';
$replacement = array(
'foo' => 'HelloMe'
);
bazFunction($pattern, $replacement); // return 'HelloMe/anotherString';
这种方法在某些框架中用作路由模式。我想知道哪个函数处理。
答案 0 :(得分:1)
$string = "foo/anotherString";
$replacement = array('foo','HelloMe');
$newString = str_replace($replacement[],,$string);
答案 1 :(得分:1)
这应该做(因为关闭需要5.3)
function my_replace($pattern, $replacement) {
// add ':' prefix to every key
$keys = array_map(function($element) {
return ':' . $element;
}, array_keys($replacement));
return str_replace($keys, array_values($replacement), $pattern);
}
如果您将内容直接传递给str_replace
str_replace(array(':foo'), array('HelloMe'), ':foo/anotherString');
答案 2 :(得分:0)
似乎php得到了你的功能......
str_replace ( ":foo", "HelloMe" , $pattern )
将为您提供此输出:HelloMe / anotherString