San San - 2 months ago 9
PHP Question

Why php code does not echo anything?

<?php
$svg_file = file_get_contents("fonts/font.svg");
$font = new DOMDocument();
$font->load($svg_file);
$glyphs = $font->getElementsByTagName('glyph');

foreach($glyphs as $g) {
echo $g->getAttribute('glyph-name');
}
?>


Where am i going wrong in above code??? it does not echo anything, trying on localhost

Answer

Try this:

$font = new DOMDocument();
$font->load("fonts/font.svg");
$glyphs = $font->getElementsByTagName('glyph');

foreach($glyphs as $g) {
    echo $g->getAttribute('glyph-name');
}

UPDATE:

$font = new DOMDocument();
$font->load("fonts/font.svg");
$glyphs = $font->getElementsByTagName('glyph');
$arr_names = array();
foreach($glyphs as $g) {
    $arr_names[] = $g->nodeValue;
}
$sort($arr_names);
foreach($arr_names as $name){
    echo $name;
}

UPDATE:

$font = new DOMDocument();
$font->load("fonts/font.svg");
$glyphs = $font->getElementsByTagName('glyph');
$arr_names = array();
foreach($glyphs as $g) {
    $arr_names[] = $g->nodeValue;
}
sort($arr_names);
foreach($arr_names as $name){
    echo $name;
}