| 1 |
<?php |
| 2 |
if (!defined('ABSPATH') && !defined('MCDATAPATH')) exit; |
| 3 |
|
| 4 |
if (!class_exists('WPRHelper')) : |
| 5 |
class WPRHelper { |
| 6 |
public static function safePregMatch($pattern, $subject, &$matches = null, $flags = 0, $offset = 0) { |
| 7 |
if (!is_string($pattern) || !is_string($subject)) { |
| 8 |
return false; |
| 9 |
} |
| 10 |
return preg_match($pattern, $subject, $matches, $flags, $offset); |
| 11 |
} |
| 12 |
|
| 13 |
# XNOTE - The below function assumes valid input |
| 14 |
# $array should be an array and $keys should be an array of string, or integer data |
| 15 |
public static function filterArray($array, $keys) { |
| 16 |
$filteredArray = array(); |
| 17 |
foreach ($keys as $key) { |
| 18 |
if (array_key_exists($key, $array)) { |
| 19 |
$filteredArray[$key] = $array[$key]; |
| 20 |
} |
| 21 |
} |
| 22 |
return $filteredArray; |
| 23 |
} |
| 24 |
|
| 25 |
# XNOTE - The below function assumes valid input |
| 26 |
# $array should be an array and $keys should be an array of string, or integer data |
| 27 |
public static function digArray($array, $keys) { |
| 28 |
if (empty($keys)) { |
| 29 |
return null; |
| 30 |
} |
| 31 |
$curr_array = $array; |
| 32 |
foreach ($keys as $key) { |
| 33 |
if (is_array($curr_array) && array_key_exists($key, $curr_array)) { |
| 34 |
$curr_array = $curr_array[$key]; |
| 35 |
} else { |
| 36 |
return null; |
| 37 |
} |
| 38 |
} |
| 39 |
return $curr_array; |
| 40 |
} |
| 41 |
|
| 42 |
public static function arrayKeyFirst($array) { |
| 43 |
if (!function_exists('array_key_first')) { |
| 44 |
foreach ($array as $key => $value) { |
| 45 |
return $key; |
| 46 |
} |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
return array_key_first($array); |
| 51 |
} |
| 52 |
} |
| 53 |
endif; |