2017-09-13 91 views
0

如果我有更多的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> 
+1

'我支持se PHP文件來讀取目錄並生成列表可以完成 - 是嗎?' - 這聽起來像這樣做 –

回答

0

你好你可以嘗試使用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 ............. ... :) –

0

該代碼會爲你

<?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); 
 
?>
工作