exploding a string with variable

拥有回忆 提交于 2020-01-16 00:56:05

问题


I'm trying to manipulate a path to an image using php. The path has a variable userdirectory in it, so i'm struggling with how to write this.

Ive used to do :

$texthtml = $content;
if (preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image) ) {
$promopic = $image['src'];
}

to find if there is an image in my content and make a variable out of it. that works, but i need to alter my code to load image from a thumbnail directory for pageload reasons.

an image src looks like this : "/uploads/userdirs/admin(variable)/image.jpg"

But i want it to be this :

"/uploads/userdirs/admin(variable)/mcith/mcith_image.jpg"

adding a /mcith/ and a mcith_prefix to the image. I'm thinking exploding it, but i dont know how to do that with a variable path. Any pointers greatly appreciated!


回答1:


You can do this many different ways, but I would probably use pathinfo() for this:

$path = pathinfo($promopic);
$thumb = $path['dirname'] . '/mcith/mcith_' . $path['basename'];



回答2:


very simple version, could use improvement

preg_replace('/\/([a-zA-Z0-9]+\.[a-zA-Z0-9]{3,4})$/', '/mcith/mcith_$1', '/uploads/userdirs/admin(variable)/image.jpg');


来源:https://stackoverflow.com/questions/10246534/exploding-a-string-with-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!