Php 使用chop和explode从文件路径提取字符串,但结果数组不';不要打印这封信;";
我正在使用Php 使用chop和explode从文件路径提取字符串,但结果数组不';不要打印这封信;";,php,arrays,string,explode,Php,Arrays,String,Explode,我正在使用explode()从特定目录中的字体文件名中提取字符串($style),然后根据生成的数组打印一些样式。如果字符串包含字母n,则结果将被截断 服务器是Ubuntu仿生运行的PHP7.3。在if(in_array())语句捕获字符串失败后,我添加了一个print\u r()语句进行调试。这时我看到字母n充当边界并截断输出 字体名称均采用$family-$style-webfont.woff(或.woff2)格式,例如merriweather bold webfont.woff 示例代码:
explode()
从特定目录中的字体文件名中提取字符串($style
),然后根据生成的数组打印一些样式。如果字符串包含字母n,则结果将被截断
服务器是Ubuntu仿生运行的PHP7.3。在if(in_array())
语句捕获字符串失败后,我添加了一个print\u 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
)
预期结果:
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
)
实际结果:
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);
看起来你只想比较一下风格。然后,您不需要在数组中使用,但可以使用if
语句
关于修剪问题:正如您在中所看到的,第二个参数说要从字符串的右侧删除哪个字母,您指定字母:,w,e,b,f,o,n,t
,因此从最右边的字母开始-如果它是修剪过的字母之一-当遇到第一个字母时,它没有停止->
因此,您将获得thi
而不是thin
作为removen
而不是i
。和hairli
而不是hairline
,n
和e
都是字母-,w,e,b,f,o,n,t
,但不是i
实例:
如果您只想(我猜这就是您想要的)删除后缀,请使用:
substr($fonf, 0, - strlen('-webfont'));
编辑:
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
)
下面是代码的示例:
$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
}
}
为什么要使用爆炸两次?应该是list($family,$style)=分解('-',$font)代码>?请注意,在当前代码中,您将family和style都设置为style var…仍在学习PHP。不知道list()
。我来试一试。问题是你的chop函数——试着用:$font=str\u replace($suffix,,$font)代码>我想你的意思是substr($font,0,-strlen('-webfont'))代码>?@克里斯:是的。您可以使用它来代替chop
功能。然后继续执行分解
和如果
检查是否有效。但是当我替换explode()
位时,像这样:foreach($files as&$file){$font=basename($file,.woff”);substr($font,0,-strlen('-webfont'));//$family=explode(“-”,$font);//$family=$family[0];//$style=explode(“-”,$font”);list($family,$style)=explode(“-”,$font);
等,它不起作用。@Chris您缺少分配给$font
-用您的代码更新了我的答案啊,我明白了-缺少“2”。尝试了更新的代码,但仍然不起作用。如果我理解列表()
的工作原理,应该。FWIW$font=str\u替换($suffix,,$font);
还可以修剪后缀,使我更容易理解代码。