php - 读出(图标-)字体的每个字符

php - read out every char of (icon-) font

我有一个(图标-)字体,想用 php 读取字体中所有可能的图标(字符)以列出它们。 是否有任何 php 函数或库使之成为可能? 字体是哪种文件类型并不重要,因为它们中的大多数都可用:svg、eot、ttf、woff。

编辑: 现在 github:https://github.com/blacksunshineCoding/svgFontReader

现在我找到了解决方案并为此构建了一个 class:

class svgFontReader {

    function listGlyphs($svgFile) {
        $allGlyphs = $this->getGlyphs($svgFile);
        return implode(',', $allGlyphs);
    }

    function getGlyphs($svgFile) {
        $svgCopy = 'font.svg';

        if (file_exists($svgCopy)) {
            unlink($svgCopy);
        }
        copy($svgFile, $svgCopy);

        $svgContent = file_get_contents($svgCopy);

        $xmlInit = simplexml_load_string($svgContent);
        $svgJson = json_encode($xmlInit);
        $svgArray = json_decode($svgJson, true);

        $svgGlyphs = $svgArray['defs']['font']['glyph'];

        if (count($svgGlyphs) > 0) {

            foreach ($svgGlyphs as $glyphId => $glyph) {
                $svgGlyphs[$glyphId] = $glyph['@attributes']['unicode'];
            }

        }

        return $svgGlyphs;
    }

}

使用如下:

$svg = dirname(__FILE__) . '/font-icons.svg'; // define font-file
$svgFontReader = new svgFontReader; // initiate class
$glyphsAll = $svgFontReader->getGlyphs($svg); // get glyphs as array
$glyphsList = $svgFontReader->listGlyphs($svg); //get glyphs as comma separate

一些规则:

  • 字符集必须是 unicode/utf8
  • 你必须在 css
  • 中包含字体
  • 对于相关区域,您必须定义字体系列