Last active
July 29, 2016 22:22
-
-
Save fjarrett/eadafb0d6519a85d70c5b1cb34838ecc to your computer and use it in GitHub Desktop.
Return an array of CSS selectors that have certain properties.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return an array of CSS selectors that have certain properties. | |
* | |
* Example: fjarrett_search_css( 'font-family: "Open Sans"' ) | |
* Result: array( 'h1', 'h2', 'h3', 'h4', 'h5', 'p', 'blockquote' ); | |
* | |
* @param string $path | |
* @param string $search | |
* | |
* @return array | |
*/ | |
function fjarrett_search_css( $path, $search ) { | |
if ( ! is_readable( $path ) ) { | |
return array(); | |
} | |
$array = array(); | |
$css = file_get_contents( $path ); | |
$css = preg_replace( '/^(\s)*(\/|\*|\#|\-|\>|\=).*(\r\n|\r|\n)?/m', '', $css ); // Strip comments | |
$css = array_map( 'trim', explode( '}', $css ) ); // Parse rule blocks | |
foreach ( $css as $rule ) { | |
$parts = array_map( 'trim', explode( '{', $rule ) ); // Parse rule | |
if ( | |
isset( $parts[1] ) // Must have a property | |
&& | |
false === strpos( $parts[0], '@' ) // No imports, media queries, etc | |
) { | |
$array[ $parts[0] ] = isset( $array[ $parts[0] ] ) ? $array[ $parts[0] ] . $parts[1] : $parts[1]; | |
} | |
} | |
$results = array_keys( preg_grep( "/{$search}/i", $array ) ); // Find case-insensitive matches | |
$results = explode( ',', implode( ',', $results ) ); // Normalize the results | |
return array_map( 'trim', array_unique( $results ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment