【问题标题】:how add font automatically into the style sheet?如何将字体自动添加到样式表中?
【发布时间】:2018-02-21 16:46:42
【问题描述】:

如果我有更多的TTF字体文件进入“字体”目录,是 有没有办法自动将它们添加到该部分? 我想一个 PHP 文件可以读取 dir 并生成列表 - 是吗?

这样(我包括这种风格,但我想添加这样的,如果文件夹有更多字体,则自动添加):

<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>

【问题讨论】:

  • I suppose a PHP file to read dir and generate the list can be done - yes? - 这听起来像是这样做的方式

标签: javascript php jquery css fonts


【解决方案1】:

您好,您可以尝试使用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>

【讨论】:

    【解决方案2】:

    此代码将为您工作

    <?php 
    
    	function getResultStyleString($directoryName)
    	{
    		$styleText = '<style type="text/css">';
    		
    		$filesList = scandir($directoryName);
    		
    		foreach($filesList as $file)
    		{
    			if(strpos($file,'.ttf') !== false)
    			{
    				$styleText .= "@font-face{font-family:" . str_replace('.ttf', '', $file).";src:url('fonts/" . $file . "')}";
    			}
    		}
    		
    		$styleText .= '</style>';
    		
    		return $styleText;
    	}
    	
    	$directoryName = "fonts/";
    	$resultStyleString = getResultStyleString($directoryName);
    	
    	var_dump($resultStyleString);
    ?>

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 2012-05-06
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2020-04-30
      • 1970-01-01
      相关资源
      最近更新 更多