我使用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被当作一些文字,像换行符之类的东西。到底怎么回事?
https://stackoverflow.com/questions/56228409
复制相似问题