2015-01-31 3 views
0

api를 호출하고 원격 URL을 내 URL로 바꿉니다. http://example.com/a/b/icon_name.gif ~ http://example.org/c/d/icon_name.png. 도메인 이름은 .gif에서 .png까지의 파일 이름 확장명으로 바뀝니다. 두 함수를 사용하는 것보다 문자열의 두 부분을 대체하는 경제적 인 방법은 무엇입니까?PHP에서 문자열 부분 만 바꾸기

$hourlyUrl = array_map(
    function($str) { 
     return str_replace('example.com/a/b/', 'example.org/c/d/', $str); 
    }, 
$hourlyUrl 
); 

$hourlyUrl = array_map(
    function($str) { 
     return str_replace('.gif', '.png', $str); 
    }, 
$hourlyUrl 
); 
+0

않는 str_replace 배열,'않는 str_replace (배열 (동의 'example.com/a/b/','.gif '), array ('example.org/c/d/ ','.png '), $ str);' – Federkun

+0

고마워, 정말 멋지다! –

답변

4
// Provides: You should eat pizza, beer, and ice cream every day 
$phrase = "You should eat fruits, vegetables, and fiber every day."; 
$healthy = array("fruits", "vegetables", "fiber"); 
$yummy = array("pizza", "beer", "ice cream"); 

$newphrase = str_replace($healthy, $yummy, $phrase); 

문서 :
http://php.net/manual/en/function.str-replace.php


귀하의 경우 :

$old_url = "http://example.com/a/b/icon_name.gif"; 
$old = array("example.com/a/b/", ".gif"); 
$new = array("example.org/c/d/", ".png"); 

$new_url = str_replace($old, $new, $old_url); 

// Output: http://example.com/c/d/icon_name.png 
+0

감사합니다. 매우 유용합니다! –

관련 문제