Associative Array of FontAwesome 4.1.0 Class Names

Thought this might help a few people. There’s likely duplicates in there so feel free to submit a change :slight_smile:

Oh great, thanks for sharing, this will sure come in handy!

Bookmarked!

Awesome! Thanks for sharing Tom

This will come in handy :slight_smile:

Isn’t it better to update the class name with a single click and save them in DB or a static file?

/**
 * Font Awesome
 *
 * @param string $path font awesomwe css file path
 * @return array
 */
function fontAwesome($path){

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

	preg_match_all($pattern, $css, $matches, PREG_SET_ORDER);
	
	$icons = array();
	foreach ($matches as $match) {
		$icons[$match[1]] = $match[2];
	}
	return $icons;

}

// Usage 
// -------------------------------------------------------
$icons = fontAwesome(PATH_TO_CSS . '/font-awesome/css/font-awesome.css');

// this will return an array with the following structure:
// -------------------------------------------------------
array(
...
[fa-glass] => \f000
...
);

// to replace the value with the name class:
// -------------------------------------------------------
$list_i = array();
foreach ($font as $key => $value) {
	$list_i[$key] = $key;
}
//Use it
print_r($list_i);

//To display the name class without prefix and dashes and Uppercase:
// -------------------------------------------------------

//Replace this:
$list_i[$key] = $key;

//with this
$list_i[$key] = ucfirst( str_ireplace(array('fa-', '-'), array('', ' '), $key) );

@Smartik, honest question, why would that method be better? Not challenging at all, just looking for your insight mate :slight_smile:

In my case, I pull the list of icons into my theme options and my page builder, so once an icon is chosen it’s saved into an option, or into meta. So the array is only called in certain admin situations anyway and then the value saved by itself.

tommusrhodus said

@Smartik, honest question, why would that method be better? Not challenging at all, just looking for your insight mate :slight_smile:

In my case, I pull the list of icons into my theme options and my page builder, so once an icon is chosen it’s saved into an option, or into meta. So the array is only called in certain admin situations anyway and then the value saved by itself.

Because you don’t have to update that list manually with every update of font awesome. In my example this will run everytime, but you can change it a bit and run it only when you want to update the list or write the array in a static file, or anything else.

It probably is not better, it is more flexible(that’s what I should say), and allow to manipulate the code in many ways. Your code is excelent for what it is intended to do, but I’ve just added another solution that allows to get the code in an easy way. :slight_smile:

Smartik said

Because you don’t have to update that list manually with every update of font awesome.

Exactly what I wanted to hear, makes total sense now :slight_smile:

Cheers mate, that’s an awesome snippet you’ve got there, thanks so much for sharing!

Thanks for sharing.