使用 chop 和 explode 从文件路径中提取字符串,但结果数组不打印字母 "n"
Using chop and explode to extract string from file path but result array doesn't print the letter "n"
我正在使用 explode()
从特定目录中的字体文件名中提取字符串 ($style
),然后根据生成的数组打印一些样式。如果字符串包含字母 n.
,结果将被截断
服务器是 Ubuntu Bionic 运行 PHP7.3。在 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 被当作一些文字,比如换行符之类的。怎么回事?
首先,我建议将语法更改为:
list($family, $style) = explode('-', $font, 2);
看来你只想比较风格。然后你不需要 in_array
但你可以只使用 if
语句。
关于您的 trim 问题:如您在 chop documentation 中所见,第二个参数说明要从字符串 - 你指定的字母:-,w,e,b,f,o,n,t
所以从最右边的字母开始 - 如果它是其中一个它 trimed - 当遇到第一个没有它的字母时停止 ->
因此您得到 thi
而不是 thin
作为移除 n
而不是 i
。 hairli
而不是 hairline
并且 n
和 e
都在字母 -,w,e,b,f,o,n,t
中而不是 i
.
实例:3v4l.
如果您想要的(我猜这就是您想要的)只是删除后缀,请使用:
substr($fonf, 0, - strlen('-webfont'));
已编辑:
这是您的代码示例:
$files = ['merriweather-regular-webfont.woff','merriweather-thin-webfont.woff','merriweather-hairline-webfont.woff'];
foreach($files as $file) {
$font = basename($file, ".woff"); // remove the file type
$font = str_replace('-webfont', '', $font); // remove the suffix
list($family, $style) = explode('-', $font, 2); // explode for 2 parts: family and style
echo "Family: $family and style: $style" . PHP_EOL;
if (in_array($style, ['thin', 'hairline'])) {
echo esc_html('font-weight:100;font-style:normal;');
} elseif (in_array($style, ['regular', 'normal'])) {
echo esc_html('font-weight:400;font-style:normal;');
} else {
echo esc_html('font-weight:400;font-style:normal;'); // Fallback
}
}
我正在使用 explode()
从特定目录中的字体文件名中提取字符串 ($style
),然后根据生成的数组打印一些样式。如果字符串包含字母 n.
服务器是 Ubuntu Bionic 运行 PHP7.3。在 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 被当作一些文字,比如换行符之类的。怎么回事?
首先,我建议将语法更改为:
list($family, $style) = explode('-', $font, 2);
看来你只想比较风格。然后你不需要 in_array
但你可以只使用 if
语句。
关于您的 trim 问题:如您在 chop documentation 中所见,第二个参数说明要从字符串 - 你指定的字母:-,w,e,b,f,o,n,t
所以从最右边的字母开始 - 如果它是其中一个它 trimed - 当遇到第一个没有它的字母时停止 ->
因此您得到 thi
而不是 thin
作为移除 n
而不是 i
。 hairli
而不是 hairline
并且 n
和 e
都在字母 -,w,e,b,f,o,n,t
中而不是 i
.
实例:3v4l.
如果您想要的(我猜这就是您想要的)只是删除后缀,请使用:
substr($fonf, 0, - strlen('-webfont'));
已编辑:
这是您的代码示例:
$files = ['merriweather-regular-webfont.woff','merriweather-thin-webfont.woff','merriweather-hairline-webfont.woff'];
foreach($files as $file) {
$font = basename($file, ".woff"); // remove the file type
$font = str_replace('-webfont', '', $font); // remove the suffix
list($family, $style) = explode('-', $font, 2); // explode for 2 parts: family and style
echo "Family: $family and style: $style" . PHP_EOL;
if (in_array($style, ['thin', 'hairline'])) {
echo esc_html('font-weight:100;font-style:normal;');
} elseif (in_array($style, ['regular', 'normal'])) {
echo esc_html('font-weight:400;font-style:normal;');
} else {
echo esc_html('font-weight:400;font-style:normal;'); // Fallback
}
}