Font-awesome: Font Awesome Icon PHP Array

Created on 5 Jun 2012  路  10Comments  路  Source: FortAwesome/Font-Awesome

Anyone have an array of all the awesome font icons including version 2.0 icons that we can use in PHP?

thanks!

enhancement

Most helpful comment

I need access to the icon names to change them on the fly based on a users choice of icons. Here is a code snippet to create a usable PHP array. This will output a string that can be used to assign to a variable.

<?php

$pattern = '/\.(icon-(?:\w+(?:-)?)+):before\s+{\s*content:\s*"(.+)";\s+}/';
$subject = file_get_contents('css/font-awesome.css');

preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);

$icons = array();

foreach($matches as $match){
    $icons[$match[1]] = $match[2];
}

$icons = var_export($icons, TRUE);
$icons = stripslashes($icons);

print_r($icons);

All 10 comments

What would be purpose of having an array of icon classes in PHP? Sorry, could not find use case so thought to ask it.

I need access to the icon names to change them on the fly based on a users choice of icons. Here is a code snippet to create a usable PHP array. This will output a string that can be used to assign to a variable.

<?php

$pattern = '/\.(icon-(?:\w+(?:-)?)+):before\s+{\s*content:\s*"(.+)";\s+}/';
$subject = file_get_contents('css/font-awesome.css');

preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);

$icons = array();

foreach($matches as $match){
    $icons[$match[1]] = $match[2];
}

$icons = var_export($icons, TRUE);
$icons = stripslashes($icons);

print_r($icons);

Thanks BernardRobbins, Your code work perfect.

:100:

In case anybody wants something a little more DRY I created a quick class that can do this for you... https://github.com/brodkinca/BCA-PHP-FontAwesomeIterator

Thanks BernardRobbins!

For anyone trying this now, be sure to change the phrase "icon-" in that script to "fa-" to work with the current FontAwesome CSS file.

Thank you @brodkin :100:

I tried

$pattern = '/\.(fa-(?:\w+(?:-)?)+):before\s+{\s*content:\s*"(.+)";\s+}/';
$subject =  file_get_contents('css/font-awesome.min.css');
preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);

But $matches is empty.
I tried using the script here and it only matches the first occurance.

Am I missing something?

@Jany-M I added 'g' to the modifiers box to the right of the expression box in your link to make it global, which meant it matched all occurrences. Is that what you're looking for?

@ryanstockau thank you :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndersDK12 picture AndersDK12  路  3Comments

seppestas picture seppestas  路  3Comments

yarcowang picture yarcowang  路  3Comments

faithdong picture faithdong  路  3Comments

rufengch picture rufengch  路  3Comments