我正在调用一个api,并用自己的URL替换了远程URL。http://example.com/a/b/icon_name.gif
到http://example.org/c/d/icon_name.png
。域名被更换,以及从文件扩展名.gif
来.png
。替换字符串的两个部分比使用两个函数更经济的方法是什么?
$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
);
// 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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句