基本的php Regex帮助改变url结构

时间:2012-12-29 20:50:11

标签: php regex

我的图像形式为:

http://sub-domain.example.net/random-folder-here/4422414324235_4234234.jpg

我想在网址中间添加一个文件夹(添加s720x720)

http://sub-domain.example.net/random-folder-here/s720x720/4422414324235_4234234.jpg

如何在php中运行正则表达式调用来帮助我这样做? 谢谢!

2 个答案:

答案 0 :(得分:1)

preg_replace('![^/]+$!', 's720x720/$0', $str);

答案 1 :(得分:1)

您希望在 dirname basename 之间添加s720x720(如果将URL作为路径)。 PHP具有在这里分开URI的功能(参见pathinfo):

$url = 'http://sub-domain.example.net/random-folder-here/4422414324235_4234234.jpg';
vprintf("%s/s720x720/%s", pathinfo($url));

输出:

http://sub-domain.example.net/random-folder-here/s720x720/4422414324235_4234234.jpg

我希望这会有所帮助。并不总是需要使用正则表达式。其他功能可能更合适。

相关问题