0

我在 php 中替换 preg 有问题。

我需要获取以以下参数结尾的文件扩展名:

../font/fontawesome-webfont.eot?v=3.2.1  
../font/fontawesome-webfont.eot?#iefix&v=3.2.1  
../font/fontawesome-webfont.woff?v=3.2.1  
../font/fontawesome-webfont.ttf?v=3.2.1  
../font/fontawesome-webfont.svg#fontawesomeregular?v=3.2.1  

ETC...

我尝试使用此功能:

pathinfo($path[1], PATHINFO_EXTENSION);

但它在点字符之后给出了最后一部分,即“1”

我尝试获取 css 文件并将 url 中的路径替换为特定路径名以进行扩展...

这是我的代码示例:

class class_name {

    private $img_exts, $font_exts;

    function __construct() {
        $this->img_exts = array('jpg','jpeg','png','gif');
        $this->font_exts = array('ttf','woff','eot','otf');
    }

    function get_css_file() {
        $css = file_get_contents('font-awesome.css');
        $css = preg_replace_callback('/url\((?!data)"?\'?(.*?)"?\'?\)/',array($this,'replace_files_callback'), $file);
        return $css;
    }

    function replace_file_callback($file) {
        $ext = pathinfo($path[1], PATHINFO_EXTENSION);
        // here is the code which i failed!!!
        $ext_niddle = preg_replace('/(.*)?(\.)?(.*)?\.([a-zA-z0-9]{2,4})\??\#?(.*)?/','$4',$path[1]);
        $fname = basename($path[1]);

        if (in_array($ext_niddle, $this->img_exts)) {
            return "url('../img/$fname')";
        } elseif (in_array($ext_niddle, $this->font_exts)) {
            return "url('../fonts/$fname')";
        } else {
            return "url('../data/$fname')";
        }
    }
}

所以我失败了!

我尝试替换它:

@font-face {
  font-family: 'FontAwesome';
  src: url('../font/fontawesome-webfont.eot?v=3.2.1');
  src: url('../font/fontawesome-webfont.eot?#iefix&v=3.2.1') format('embedded-opentype'), url('../font/fontawesome-webfont.woff?v=3.2.1') format('woff'), url('../font/fontawesome-webfont.ttf?v=3.2.1') format('truetype'), url('../font/fontawesome-webfont.svg#fontawesomeregular?v=3.2.1') format('svg');
  font-weight: normal;
  font-style: normal;
}

对此:

@font-face {
  font-family: 'FontAwesome';
  src: url('../fonts/fontawesome-webfont.eot?v=3.2.1');
  src: url('../fonts/fontawesome-webfont.eot?#iefix&v=3.2.1') format('embedded-opentype'), url('../fonts/fontawesome-webfont.woff?v=3.2.1') format('woff'), url('../fonts/fontawesome-webfont.ttf?v=3.2.1') format('truetype'), url('../data/fontawesome-webfont.svg#fontawesomeregular?v=3.2.1') format('svg');
  font-weight: normal;
  font-style: normal;
}

我对文件名很好奇: ../folder/font.name.ttf?ver=1.2.3 或类似的东西......

如果您发现不合适的代码,请帮助我!

我将在这个项目中使用它:http: //jslibgen.com/

提前致谢...

4

2 回答 2

0
<?php
    $uri = "../font/fontawesome-webfont.eot?v=3.2.1";

    preg_match("/\/(.+?)\.(.+?)(#|\?)/", $uri, $m);
    $ext = $m[2];

    echo $ext;
?>
于 2013-09-01T07:45:33.613 回答
0

如何使用parse_url

$data = array(
'../font/fontawesome-webfont.eot?v=3.2.1',
'../font/fontawesome-webfont.eot?#iefix&v=3.2.1',
'../font/fontawesome-webfont.woff?v=3.2.1',
'../font/fontawesome-webfont.ttf?v=3.2.1',
'../font/fontawesome-webfont.svg#fontawesomeregular?v=3.2.1',
);
foreach ($data as $url) {
    $parse = parse_url($url);
    $fname = basename($parse['path']);
    $ext = pathinfo($fname,  PATHINFO_EXTENSION);
    echo "fname=$fname\text=$ext\n";
}

输出:

fname=fontawesome-webfont.eot   ext=eot
fname=fontawesome-webfont.eot   ext=eot
fname=fontawesome-webfont.woff  ext=woff
fname=fontawesome-webfont.ttf   ext=ttf
fname=fontawesome-webfont.svg   ext=svg
于 2013-09-01T08:53:32.573 回答