首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用chop和explode从文件路径提取字符串,但结果数组不打印字母"n“

使用chop和explode从文件路径提取字符串,但结果数组不打印字母"n“
EN

Stack Overflow用户
提问于 2019-05-21 05:29:21
回答 1查看 72关注 0票数 2

我使用explode()从特定目录中的字体文件名中提取一个字符串($style),然后根据生成的数组打印一些样式。如果字符串包含字母n,则结果将被截断。

服务器是运行PHP7.3的Ubuntu Bionic。在if (in_array())语句未能捕获字符串之后,我添加了一条要调试的print_r()语句。这时,我看到字母n充当边界并截断输出。

字体名称都采用$family-$style-webfont.woff (或.woff2)格式,例如merriweather-bold-webfont.woff

示例代码:

function my_fontload() {
    // Locate font files
    $font_path = get_stylesheet_directory_uri() . "/path/to/fonts/";
    $files = glob(get_stylesheet_directory( __FILE__ ) . '/path/to/fonts/*.woff', GLOB_BRACE);

    $suffix = '-webfont';
      foreach($files as &$file) {

        $font = basename($file, ".woff");
        $font = chop($font,$suffix);

        $family = explode("-", $font);
        $family = $family[0];

        $style = explode("-", $font);


        echo '@font-face{font-family:\''.$family.'\';src:url('. esc_url(( $font_path).basename($file)).'2)format(\'woff2\'),url('.esc_url(( $font_path).basename($file)).')format(\'woff\');';
        if (in_array('thin', $style) || in_array( 'hairline', $style)) {
            // Do stuff
        } elseif (in_array('regular', $style) || in_array( 'normal', $style)) {
           // Do other stuff
        } else {
            // Do default stuff
        }
        // Other logic here

        // debugging
        print_r($style);
    }

    unset ($file);
}

预期结果:

(
    [0] => merriweather
    [1] => regular
)

(
    [0] => merriweather
    [1] => thin
)

(
    [0] => merriweather
    [1] => hairline
)

实际结果:

(
    [0] => merriweather
    [1] => regular
)

(
    [0] => merriweather
    [1] => thi
)

(
    [0] => merriweather
    [1] => hairli
)

这就好像n被当作一些文字,像换行符之类的东西。到底怎么回事?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56228409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档