【问题标题】:Append string at the end of url before extension [duplicate]在扩展名之前的 url 末尾附加字符串 [重复]
【发布时间】:2017-07-12 01:32:35
【问题描述】:
<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"

【问题讨论】:

    标签: php url append


    【解决方案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;
    

    【讨论】:

      【解决方案2】:

      如果文件名来自数据库,并且文件系统中可能不存在文件,您可以使用substr_replace()在文件名之间插入特定字符串:

      <?php
      $string = 'new-thumb-01.jpg';
      $replacement = '-big';
      echo substr_replace( $string , $replacement , strrpos($string, '.'), 0 );
      

      【讨论】:

        【解决方案3】:

        你可以简单地使用explode()函数

        $url = "images/new-thumb-01.jpg";
        $image = explode('.', $url);
        $newUrl =  $image[0].'-big.'.$image[1]; 
        echo $newUrl;
        

        希望对你有帮助..

        【讨论】:

          【解决方案4】:

          你可以使用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

          【讨论】:

          • 我喜欢这个解决方案,因为如果 url 中除了扩展名之前的点之外还有点,那么下面的分解解决方案可能会中断。并且由于 pathinfo 将 url 分解为一个数组,因此如果需要,我可以使用 $fileparts['dirname'] 在 url 的目录部分添加回。超级有帮助。谢谢!
          猜你喜欢
          • 2017-09-29
          • 1970-01-01
          • 2022-06-10
          • 1970-01-01
          • 2021-12-16
          • 2012-06-15
          • 1970-01-01
          • 2022-12-11
          • 1970-01-01
          相关资源
          最近更新 更多