Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP regex extraxt min-width / max-width from CSS MediaQuery
<?php
function getMinMaxFromMediaQuery($mediaQuery) {
$re = '/\d*(min-width|max-width):\s*(\d+\s?)(px|em|rem)/';
preg_match_all($re, $mediaQuery, $matches, PREG_SET_ORDER, 0);
$result = array();
if (!empty($matches)) {
if (count($matches) <= 2) {
foreach ($matches as $match) {
if (count($match) == 4) {
$result[] = [
'mediaQuery' => $mediaQuery,
'type' => $match[1], // min-width / max-width
'size' => $match[2], // 1200
'unit' => $match[3], // px
];
}
}
}
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment