0

If I have more TTF font files into the "fonts" directory, is there a way to automatically add them into the section? I suppose a PHP file to read dir and generate the list can be done - yes?

like this (i include this style, but i want add this like, automatically if the folder have more fonts):

<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
    `I suppose a PHP file to read dir and generate the list can be done - yes?` - this sounds like the way to do it – Jaromanda X Sep 13 '17 at 05:41

2 Answers2

0

Hi You could try using scandir(). I have been looking this answer from Emil Vikström:

Loop code for each file in a directory

Once you have this maybe this can give you an idea:

<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>
Abel
  • 53
  • 6
0

This code will work for you

<?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);
?>
Dmitriy Buteiko
  • 624
  • 1
  • 6
  • 14