mirror of
https://github.com/getgrav/grav.git
synced 2025-12-05 15:29:57 +01:00
couple of helpers
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user