dpklt4291 2013-09-10 17:02
浏览 41
已采纳

从字符串codeigniter资源加载器中查找扩展名

I have a model in codeigniter in which I basically want to be able to input a file or array of files into the function and then render the proper html for that file. I am using strchr to find the last occurrence of the . and then get everything after. However this is not working as I figured it would according to php documentation (the function always returns false).

Here is the model:

<?php

class Asset_Load_Model extends CI_Model {

    public static $jsWrapperStart = '<script type="text/javascript" src="';
    public static $jsWrapperEnd = '"></script>';
    public static $cssWrapperStart = '<link rel="stylesheet" type="text/css" src="';
    public static $cssWrapperEnd = '">';
    public static $imageTypes = array ( 'gif', 'jpeg', 'png', 'swf', 'psd', 'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpf', 'jb2', 'swc', 'aiff', 'wbmp', 'xbm');

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
    }
    /**
     * Load an asset based on extension in string
     *
     * @param string/array $file
     * @param string $class for image types only
     */
    public function loadAsset($file, $class = '') {
        $out = '';
        if(is_array($file)) {
            $files = array();
            foreach ($files as $file) {
                $out .= $this->loadAsset($file);
            }
            return $out;
        } else {
            $ext = strrchr($file, '.');
            if ($ext == 'css') {
                return self::cssWrapperStart . base_url() . $file . self::cssWrapperEnd;
            } elseif ($ext == 'js') {
                return self::jsWrapperStart . base_url() . $file . self::jsWrapperEnd;
            } elseif (in_array($ext, self::$imageTypes)) {
                $class = isset($class) ? ' class="' . $class . '"' : '';
                return '<img alt="" src="' . base_url() . $file . '"' . $class . '>';
            } else {
                return false;
            }
        }
    }
}

?>

Sample usage:

$dataFooter['page_level_plugins'] = $this->asset_load_model->loadAsset(array('assets/plugins/bootstrap-modal/js/bootstrap-modal.js', 'assets/plugins/bootstrap-modal/js/bootstrap-modalmanager.js', 'assets/plugins/jquery-validation/dist/jquery.validate.min.js', 'assets/plugins/select2/select2.min.js'));
  • 写回答

2条回答 默认 最新

  • douju9272 2013-09-10 17:31
    关注
    $files = array();
    foreach ($files as $file) {
        $out .= $this->loadAsset($file);
    }
    

    You are declaring $files as an empty array before the foreach and as such aren't sending anything to loadAssets.

    Try something like

    foreach ($file as $f) {
        $out .= $this->loadAsset($f);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度