RegEx-如何在文件扩展名之前插入字符串 [英] RegEx - How To Insert String Before File Extension
问题描述
如何在动态生成的文件中插入"_thumb".
How would I insert "_thumb" into files that are being dyanmically generated.
例如,我有一个允许用户上传图像的网站.该脚本获取图像,对其进行优化并保存到文件中.如何为优化后的图像插入字符串"_thumb"?
For example, I have a site that allows users to upload an image. The script takes the image, optimizes it and saves to file. How would I make it insert the string "_thumb" for the optimized image?
我当前正在保存otpimized文件的1个版本. ch-1268312613-photo.jpg
I'm currently saving 1 version of the otpimized file. ch-1268312613-photo.jpg
我想将原始文件另存为上述字符串,但要像以下字符串一样附加"_thumb" ch-1268312613-photo_thumb.jpg
I want to save the original as the above string, but want to append, "_thumb" like the following string ch-1268312613-photo_thumb.jpg
推荐答案
无需使用正则表达式.可以做到这一点:
No need to use regex. This will do it:
$extension_pos = strrpos($filename, '.'); // find position of the last dot, so where the extension starts
$thumb = substr($filename, 0, $extension_pos) . '_thumb' . substr($filename, $extension_pos);
这篇关于RegEx-如何在文件扩展名之前插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!