如何将字体自动添加到样式表中?
问题描述:
如果我有更多的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()。我一直在寻找从埃米尔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>
+0
谢谢mhhhan ..... it works ............. ... :) –
答
工作
该代码会为你
<?php
\t function getResultStyleString($directoryName)
\t {
\t \t $styleText = '<style type="text/css">';
\t \t
\t \t $filesList = scandir($directoryName);
\t \t
\t \t foreach($filesList as $file)
\t \t {
\t \t \t if(strpos($file,'.ttf') !== false)
\t \t \t {
\t \t \t \t $styleText .= "@font-face{font-family:" . str_replace('.ttf', '', $file).";src:url('fonts/" . $file . "')}";
\t \t \t }
\t \t }
\t \t
\t \t $styleText .= '</style>';
\t \t
\t \t return $styleText;
\t }
\t
\t $directoryName = "fonts/";
\t $resultStyleString = getResultStyleString($directoryName);
\t
\t var_dump($resultStyleString);
?>
'我支持se PHP文件来读取目录并生成列表可以完成 - 是吗?' - 这听起来像这样做 –