| 1 |
<?php |
| 2 |
/** |
| 3 |
* String utilities. |
| 4 |
* |
| 5 |
* Copyright: © 2009-2011 |
| 6 |
* {@link http://www.websharks-inc.com/ WebSharks, Inc.} |
| 7 |
* ( coded in the USA ) |
| 8 |
* |
| 9 |
* Released under the terms of the GNU General Public License. |
| 10 |
* You should have received a copy of the GNU General Public License, |
| 11 |
* along with this software. In the main directory, see: /licensing/ |
| 12 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 13 |
* |
| 14 |
* @package s2Member\Utilities |
| 15 |
* @since 3.5 |
| 16 |
*/ |
| 17 |
if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"])) |
| 18 |
exit ("Do not access this file directly."); |
| 19 |
/**/ |
| 20 |
if (!class_exists ("c_ws_plugin__s2member_utils_strings")) |
| 21 |
{ |
| 22 |
/** |
| 23 |
* String utilities. |
| 24 |
* |
| 25 |
* @package s2Member\Utilities |
| 26 |
* @since 3.5 |
| 27 |
*/ |
| 28 |
class c_ws_plugin__s2member_utils_strings |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Escapes double quotes. |
| 32 |
* |
| 33 |
* @package s2Member\Utilities |
| 34 |
* @since 3.5 |
| 35 |
* |
| 36 |
* @param str $string Input string. |
| 37 |
* @return str Output string after double quotes are escaped. |
| 38 |
*/ |
| 39 |
public static function esc_dq ($string = FALSE) |
| 40 |
{ |
| 41 |
return str_replace ('"', '\"', (string)$string); |
| 42 |
} |
| 43 |
/** |
| 44 |
* Escapes single quotes. |
| 45 |
* |
| 46 |
* @package s2Member\Utilities |
| 47 |
* @since 3.5 |
| 48 |
* |
| 49 |
* @param str $string Input string. |
| 50 |
* @return str Output string after single quotes are escaped. |
| 51 |
*/ |
| 52 |
public static function esc_sq ($string = FALSE) |
| 53 |
{ |
| 54 |
return str_replace ("'", "\'", (string)$string); |
| 55 |
} |
| 56 |
/** |
| 57 |
* Escapes dollars signs ( for regex patterns ). |
| 58 |
* |
| 59 |
* @package s2Member\Utilities |
| 60 |
* @since 3.5 |
| 61 |
* |
| 62 |
* @param str $string Input string. |
| 63 |
* @return str Output string after dollar signs are escaped. |
| 64 |
*/ |
| 65 |
public static function esc_ds ($string = FALSE) |
| 66 |
{ |
| 67 |
return str_replace ('$', '\\$', (string)$string); |
| 68 |
} |
| 69 |
/** |
| 70 |
* Sanitizes a string; by removing non-standard characters. |
| 71 |
* |
| 72 |
* This allows all characters that appears on a standard U.S. keyboard. |
| 73 |
* |
| 74 |
* @package s2Member\Utilities |
| 75 |
* @since 3.5 |
| 76 |
* |
| 77 |
* @param str $string Input string. |
| 78 |
* @return str Output string after non-keyboard chars are removed. |
| 79 |
*/ |
| 80 |
public static function keyboard_chars_only ($string = FALSE) |
| 81 |
{ |
| 82 |
return preg_replace ("/[^0-9A-Z\r\n\t\s`\=\[\]\\\;',\.\/~\!@#\$%\^&\*\(\)_\+\|\}\{\:\"\?\>\<\-]/i", "", remove_accents ((string)$string)); |
| 83 |
} |
| 84 |
/** |
| 85 |
* Trims deeply. |
| 86 |
* |
| 87 |
* @package s2Member\Utilities |
| 88 |
* @since 3.5 |
| 89 |
* |
| 90 |
* @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values. |
| 91 |
* @return str|array Either the input string, or the input array; after all data is trimmed up. |
| 92 |
*/ |
| 93 |
public static function trim_deep ($value = FALSE) |
| 94 |
{ |
| 95 |
return is_array ($value) ? array_map ("c_ws_plugin__s2member_utils_strings::trim_deep", $value) : trim ((string)$value); |
| 96 |
} |
| 97 |
/** |
| 98 |
* Trims " entities deeply. |
| 99 |
* |
| 100 |
* This is useful on Shortcode attributes mangled by a Visual Editor. |
| 101 |
* |
| 102 |
* @package s2Member\Utilities |
| 103 |
* @since 3.5 |
| 104 |
* |
| 105 |
* @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values. |
| 106 |
* @return str|array Either the input string, or the input array; after all data is trimmed up. |
| 107 |
*/ |
| 108 |
public static function trim_quot_deep ($value = FALSE) |
| 109 |
{ |
| 110 |
return is_array ($value) ? array_map ("c_ws_plugin__s2member_utils_strings::trim_quot_deep", $value) : preg_replace ("(^(")+|(")+$)", "", (string)$value); |
| 111 |
} |
| 112 |
/** |
| 113 |
* Trims double quotes deeply. |
| 114 |
* |
| 115 |
* This is useful on CSV data that is encapsulated by double quotes. |
| 116 |
* |
| 117 |
* @package s2Member\Utilities |
| 118 |
* @since 3.5 |
| 119 |
* |
| 120 |
* @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values. |
| 121 |
* @return str|array Either the input string, or the input array; after all data is trimmed up. |
| 122 |
*/ |
| 123 |
public static function trim_dq_deep ($value = FALSE) |
| 124 |
{ |
| 125 |
return is_array ($value) ? array_map ("c_ws_plugin__s2member_utils_strings::trim_dq_deep", $value) : trim ((string)$value, "\" \t\n\r\0\x0B"); |
| 126 |
} |
| 127 |
/** |
| 128 |
* Wraps a string with the characters provided. |
| 129 |
* |
| 130 |
* This is useful when preparing an input array for ``c_ws_plugin__s2member_utils_arrays::in_regex_array()``. |
| 131 |
* |
| 132 |
* @package s2Member\Utilities |
| 133 |
* @since 3.5 |
| 134 |
* |
| 135 |
* @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values. |
| 136 |
* @param str $beg A string value to wrap at the beginning of each value. |
| 137 |
* @param str $end A string value to wrap at the ending of each value. |
| 138 |
* @return str|array Either the input string, or the input array; after all data is wrapped up. |
| 139 |
*/ |
| 140 |
public static function wrap_deep ($value = FALSE, $beg = FALSE, $end = FALSE) |
| 141 |
{ |
| 142 |
return (is_array ($value) && !empty ($value)) ? array_map ("c_ws_plugin__s2member_utils_strings::wrap_deep", $value, array_fill (0, count ($value), $beg), array_fill (0, count ($value), $end)) : ((is_string ($value) && strlen ($value)) ? (string)$beg . (string)$value . (string)$end : ((is_array ($value)) ? $value : (string)$value)); |
| 143 |
} |
| 144 |
/** |
| 145 |
* Generates a random string with letters/numbers/symbols. |
| 146 |
* |
| 147 |
* @package s2Member\Utilities |
| 148 |
* @since 3.5 |
| 149 |
* |
| 150 |
* @param int $length Length of the randomly generated string. |
| 151 |
* @param bool $special_chars Defaults to true. If false, special chars are NOT included. |
| 152 |
* @param bool $extra_special_chars Defaults to false. If true, extra special chars are included. |
| 153 |
* @return str A randomly generated string, based on parameter configuration. |
| 154 |
*/ |
| 155 |
public static function random_str_gen ($length = 12, $special_chars = TRUE, $extra_special_chars = FALSE) |
| 156 |
{ |
| 157 |
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 158 |
$chars .= ($extra_special_chars) ? "-_ []{}<>~`+=,.;:/?|" : ""; |
| 159 |
$chars .= ($special_chars) ? "!@#$%^&*()" : ""; |
| 160 |
/**/ |
| 161 |
for ($i = 0, $random_str = ""; $i < $length; $i++) |
| 162 |
$random_str .= substr ($chars, mt_rand (0, strlen ($chars) - 1), 1); |
| 163 |
/**/ |
| 164 |
return $random_str; |
| 165 |
} |
| 166 |
/** |
| 167 |
* Highlights PHP, and also Shortcodes. |
| 168 |
* |
| 169 |
* @package s2Member\Utilities |
| 170 |
* @since 3.5 |
| 171 |
* |
| 172 |
* @param str $str Input string to be highlighted. |
| 173 |
* @return str The highlighted string. |
| 174 |
*/ |
| 175 |
public static function highlight_php ($str = FALSE) |
| 176 |
{ |
| 177 |
$str = highlight_string ($str, true); /* Start with PHP syntax highlighting first. */ |
| 178 |
/**/ |
| 179 |
return preg_replace_callback ("/(\[)(\/?)(_*s2If|s2Get|s2Member-[A-z_0-9\-]+)(.*?)(\])/i", "c_ws_plugin__s2member_utils_strings::_highlight_php", $str); |
| 180 |
} |
| 181 |
/** |
| 182 |
* Highlights Shortcodes. |
| 183 |
* |
| 184 |
* @package s2Member\Utilities |
| 185 |
* @since 3.5 |
| 186 |
* |
| 187 |
* @param array $m Array passed in from `preg_replace_callback()`. |
| 188 |
* @return str The highlighted string. |
| 189 |
*/ |
| 190 |
public static function _highlight_php ($m = FALSE) |
| 191 |
{ |
| 192 |
return '<span style="color:#164A61;">' . $m[0] . '</span>'; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
?> |