如果我在“fonts”目录下有更多的TTF字体文件,是 有办法自动将它们添加到该部分吗? 我想一个 PHP 文件可以读取目录并生成列表 - 是吗?
像这样(我包括这种风格,但我想添加这样的,如果文件夹有更多字体则自动添加):
<style type="text/css">
@font-face
{
font-family: Baksoda;
src: url('fonts/Baksoda.ttf');
}
@font-face
{
font-family: CherrySwash-Regular;
src: url('fonts/CherrySwash-Regular.ttf');
}
@font-face
{
font-family: Riya-Black;
src: url('fonts/Riya-Black.ttf');
}
</style>
最佳答案
您好,您可以尝试使用 scandir() .我一直在寻找 Emil Vikström 的回答:
Loop code for each file in a directory
一旦你有了这个,也许这可以给你一个想法:
<style>
<?php
$files = scandir('fonts/');
foreach($files as $file){
if(strpos($file,'.ttf') !== false){
echo "@font-face{font-family:".str_replace('.ttf','',$file).";src:url('fonts/".$file."')}";
}
}
?>
</style>
关于javascript - 如何自动将字体添加到样式表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46189646/