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


Basic php Regex Help for changing url structure

我有一个图像的形式:

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

,我想在url中间添加一个文件夹,像这样(s720x720 added)

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

我如何在php中运行一个正则表达式调用,这将帮助我做到这一点?谢谢!

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

您需要在dirnamebasename之间添加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

我希望这对你有帮助。并不总是需要使用正则表达式。其他函数可以更合适。