在文件扩展名之前添加字符串到URL的末尾

3
<a class="fancybox" href="images/<?php echo $prod['item_image_url']; ?>" data-fancybox-group="gallery" title=""><img src="images/<?php echo $prod['item_image_url']; ?>" alt="" /></a>

在这段代码中,echo $prod['item_image_url']; 打印出存储在我的表中的图片URL,并打印如下:
href="images/new-thumb-01.jpg"

我正在尝试将此转换为:
href="images/new-thumb-01-big.jpg"

不更改数据库条目。这意味着在url末尾但在".jpg"之前添加"-big"

4个回答

8
您可以使用 pathinfo 函数:
$prod['item_image_url'] = "new-thumb-01.jpg";

$fileparts = pathinfo($prod['item_image_url']);

$bigFileName = $fileparts['filename'] . "-big." .$fileparts['extension'];

$bigFileName保存着new-thumb-01-big.jpg


2
{btsdaf} - RachieVee

3
你可以简单地使用explode()函数。
$url = "images/new-thumb-01.jpg";
$image = explode('.', $url);
$newUrl =  $image[0].'-big.'.$image[1]; 
echo $newUrl;

希望这能帮到你。

1
如果文件名来自数据库,而文件在文件系统中可能不存在,则可以使用substr_replace()将特定字符串插入文件名之间:
<?php
$string = 'new-thumb-01.jpg';
$replacement = '-big';
echo substr_replace( $string , $replacement , strrpos($string, '.'), 0 );

1
 You have to break filename and file extension and then add whatever file name 
 before extension.

 $fileOrg = $_FILES[0]['name'];
 $filenameOnly = array_pop(array_reverse(explode(".", $fileOrg )));
 $ext = end(explode('.', $fileOrg ));
 $filename = $filenameOnly . '-big' .'.' . $ext;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接