2012-07-30 2 views
-3

나는 이와 같은 핸들 교체를 위해 PHP에서 함수가 필요합니다.PHP 대체 경로 시스템

$pattern = ':foo/anotherString'; 

$replacement = array(
    'foo' => 'HelloMe' 
); 

bazFunction($pattern, $replacement); // return 'HelloMe/anotherString'; 

이 방법은 일부 프레임 워크에서 경로 패턴으로 사용됩니다. 나는 어떤 기능을 처리 알고 있습니다. HelloMe/anotherString이 무엇을해야

+0

쓸모 없거나 명확하지 않습니까? – sweb

답변

1
$string = "foo/anotherString"; 
$replacement = array('foo','HelloMe'); 
$newString = str_replace($replacement[],,$string); 
+0

당신은'$ string'에서':'를 놓쳤습니다. – ManseUK

0

PHP는 당신을위한 기능 ...

str_replace (":foo", "HelloMe" , $pattern) 

당신이 출력을 줄 것이다있어 보인다

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); 
} 

에 직접 전달한 경우이 기능이 필요하지 않습니다.210

str_replace(array(':foo'), array('HelloMe'), ':foo/anotherString');