couple of helpers

This commit is contained in:
Andy Miller
2019-03-11 12:14:21 -06:00
parent a2ac3f4c8b
commit dd9aa5bf78

View File

@@ -147,6 +147,30 @@ abstract class Utils
return $status;
}
/**
* Function that can match wildcards
*
* match_wildcard('foo*', $test), // TRUE
* match_wildcard('bar*', $test), // FALSE
* match_wildcard('*bar*', $test), // TRUE
* match_wildcard('**blob**', $test), // TRUE
* match_wildcard('*a?d*', $test), // TRUE
* match_wildcard('*etc**', $test) // TRUE
*
* @param $wildcard_pattern
* @param $haystack
* @return false|int
*/
public static function matchWildcard( $wildcard_pattern, $haystack) {
$regex = str_replace(
array("\*", "\?"), // wildcard chars
array('.*','.'), // regexp chars
preg_quote($wildcard_pattern)
);
return preg_match('/^'.$regex.'$/is', $haystack);
}
/**
* Returns the substring of a string up to a specified needle. if not found, return the whole haystack
*
@@ -348,6 +372,24 @@ abstract class Utils
return $date_formats;
}
/**
* Get current date/time
*
* @param null $default_format
* @return string
* @throws \Exception
*/
public static function dateNow($default_format = null)
{
$now = new \DateTime();
if (is_null($default_format)) {
$default_format = Grav::instance()['config']->get('system.pages.dateformat.default');
}
return $now->format($default_format);
}
/**
* Truncate text by number of characters but can cut off words.
*