admin
3 months ago
field-groups
3 months ago
fields
1 week ago
fields-settings
3 months ago
forms
3 months ago
locations
3 months ago
modules
1 week ago
screens
3 months ago
acfe-deprecated-functions.php
2 years ago
acfe-field-functions.php
3 months ago
acfe-field-group-functions.php
3 months ago
acfe-file-functions.php
1 year ago
acfe-form-functions.php
3 months ago
acfe-helper-array-functions.php
3 months ago
acfe-helper-functions.php
3 months ago
acfe-helper-multi-functions.php
3 months ago
acfe-helper-string-functions.php
3 months ago
acfe-meta-functions.php
3 months ago
acfe-post-functions.php
3 months ago
acfe-screen-functions.php
3 months ago
acfe-template-functions.php
3 months ago
acfe-term-functions.php
3 months ago
acfe-user-functions.php
3 months ago
acfe-wp-functions.php
3 years ago
assets.php
3 months ago
compatibility-acf-5.8.php
4 months ago
compatibility-acf-5.9.php
3 months ago
compatibility-acf-6.5.php
3 months ago
compatibility-acf-6.8.6.php
1 week ago
compatibility.php
3 months ago
field-extend.php
4 months ago
field.php
4 months ago
hooks.php
3 months ago
init.php
3 months ago
local-meta.php
3 years ago
media.php
3 months ago
module-acf.php
3 months ago
module-db.php
3 months ago
module-l10n.php
3 months ago
module-legacy.php
3 years ago
module-manager.php
4 months ago
module-post.php
3 months ago
module-posts.php
4 months ago
module-upgrades.php
3 years ago
module.php
3 months ago
multilang.php
3 months ago
revisions.php
4 months ago
screen.php
3 months ago
settings.php
3 months ago
template-tags.php
3 months ago
third-party.php
3 years ago
upgrades.php
3 months ago
acfe-helper-string-functions.php
335 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * acfe_as_string |
| 9 | * |
| 10 | * @param $var |
| 11 | * @param $delimiter |
| 12 | * |
| 13 | * @return string |
| 14 | */ |
| 15 | function acfe_as_string($var, $delimiter = ''){ |
| 16 | |
| 17 | // already a string |
| 18 | if(is_string($var)){ |
| 19 | return $var; |
| 20 | } |
| 21 | |
| 22 | // basic type return empty string |
| 23 | if($var === true || $var === false || $var === null){ |
| 24 | return ''; |
| 25 | } |
| 26 | |
| 27 | // array |
| 28 | if(is_array($var) && $delimiter !== null){ |
| 29 | |
| 30 | // flatten array and remove empty values |
| 31 | $flatten = acfe_array_flatten($var); |
| 32 | $flatten = acfe_array_where($flatten, function($value){ |
| 33 | return is_numeric($value) || (is_string($value) && !empty($value)); |
| 34 | }); |
| 35 | |
| 36 | return implode($delimiter, $flatten); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | // object |
| 41 | if(is_object($var)){ |
| 42 | return method_exists($var, '__toString') ? (string) $var : ''; |
| 43 | } |
| 44 | |
| 45 | // return |
| 46 | return strval($var); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * acfe_number_suffix |
| 53 | * |
| 54 | * Adds 1"st", 2"nd", 3"rd" to number |
| 55 | * |
| 56 | * @param $num |
| 57 | * |
| 58 | * @return string |
| 59 | */ |
| 60 | function acfe_number_suffix($num){ |
| 61 | |
| 62 | if(!in_array(($num % 100), array(11, 12, 13))){ |
| 63 | |
| 64 | switch($num % 10){ |
| 65 | case 1: return $num . 'st'; |
| 66 | case 2: return $num . 'nd'; |
| 67 | case 3: return $num . 'rd'; |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | |
| 72 | return $num . 'th'; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * acfe_is_json |
| 79 | * |
| 80 | * Check if the string is a json input |
| 81 | * https://stackoverflow.com/a/6041773 |
| 82 | * |
| 83 | * @param $string |
| 84 | * |
| 85 | * @return bool |
| 86 | */ |
| 87 | function acfe_is_json($string){ |
| 88 | |
| 89 | // in case string = 1 or not string |
| 90 | if(is_numeric($string) || !is_string($string)){ |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | // decode |
| 95 | json_decode($string); |
| 96 | |
| 97 | // check if decode has errors |
| 98 | return json_last_error() == JSON_ERROR_NONE; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * acfe_is_html |
| 105 | * |
| 106 | * Check if string is html |
| 107 | * https://subinsb.com/php-check-if-string-is-html/ |
| 108 | * |
| 109 | * @param $string |
| 110 | * |
| 111 | * @return bool |
| 112 | */ |
| 113 | function acfe_is_html($string){ |
| 114 | return $string !== strip_tags($string); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * acfe_str_rtrim |
| 120 | * |
| 121 | * Remove a suffix from a string |
| 122 | * |
| 123 | * @param $subject |
| 124 | * @param $search |
| 125 | * |
| 126 | * @return false|mixed|string |
| 127 | */ |
| 128 | function acfe_str_rtrim($subject, $search){ |
| 129 | |
| 130 | // validate |
| 131 | if(!is_string($subject)){ |
| 132 | return $subject; |
| 133 | } |
| 134 | |
| 135 | $length = strlen($search); |
| 136 | if(substr($subject, -$length) === $search){ |
| 137 | return substr($subject, 0, -$length); |
| 138 | } |
| 139 | |
| 140 | return $subject; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * acfe_str_ltrim |
| 146 | * |
| 147 | * Remove a prefix from a string |
| 148 | * |
| 149 | * @param $subject |
| 150 | * @param $search |
| 151 | * |
| 152 | * @return string |
| 153 | */ |
| 154 | function acfe_str_ltrim($subject, $search){ |
| 155 | |
| 156 | // validate |
| 157 | if(!is_string($subject)){ |
| 158 | return $subject; |
| 159 | } |
| 160 | |
| 161 | $length = strlen($search); |
| 162 | if(substr($subject, 0, $length) === $search){ |
| 163 | return substr($subject, $length); |
| 164 | } |
| 165 | |
| 166 | return $subject; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /** |
| 171 | * acfe_str_starts_with |
| 172 | * |
| 173 | * Check if a strings starts with something |
| 174 | * |
| 175 | * @param $subject |
| 176 | * @param $search |
| 177 | * |
| 178 | * @return bool |
| 179 | */ |
| 180 | function acfe_str_starts_with($subject, $search){ |
| 181 | |
| 182 | // validate |
| 183 | if(!is_string($subject)){ |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | $length = strlen($search); |
| 188 | return substr($subject, 0, $length) === $search; |
| 189 | |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * acfe_str_ends_with |
| 194 | * |
| 195 | * Check if a strings ends with something |
| 196 | * |
| 197 | * @param $subject |
| 198 | * @param $search |
| 199 | * |
| 200 | * @return bool |
| 201 | */ |
| 202 | function acfe_str_ends_with($subject, $search){ |
| 203 | |
| 204 | $length = strlen($search); |
| 205 | if($length === 0){ |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | // validate |
| 210 | if(!is_string($subject)){ |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | return substr($subject, -$length) === $search; |
| 215 | |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * acfe_str_replace |
| 221 | * |
| 222 | * Replace occurrences of a search string with a replacement string, with support for offset and limit. |
| 223 | * |
| 224 | * If offset is 0, it will replace the first occurrence of the search string. |
| 225 | * If offset is 1, it will replace the second occurrence of the search string, and so on. |
| 226 | * If offset is -1 it will replace the last occurrence of the search string |
| 227 | * If offset is -2 it will replace the second to last occurrence of the search string, and so on. |
| 228 | * |
| 229 | * Limit is the maximum number of replacements to perform. |
| 230 | * If limit is 0, it will replace all occurrences of the search string after the offset. |
| 231 | * |
| 232 | * If delete_others is true, it will delete the other occurrences of the search string after the all occurences have been replaced following the offset + limit rule. |
| 233 | * |
| 234 | * @param $subject |
| 235 | * @param $search |
| 236 | * @param $replace |
| 237 | * @param $offset |
| 238 | * @param $limit |
| 239 | * @param $delete_others |
| 240 | * |
| 241 | * @return array|mixed|string|string[] |
| 242 | */ |
| 243 | function acfe_str_replace($subject, $search, $replace, $offset = 0, $limit = 0, $delete_others = false){ |
| 244 | |
| 245 | // validate |
| 246 | if(!is_string($subject) || $search === ''){ |
| 247 | return $subject; |
| 248 | } |
| 249 | |
| 250 | // limit must be always at minimum 0 |
| 251 | $limit = max(0, $limit); |
| 252 | |
| 253 | // shortcircuit: if limit is 0 and offset is 0, replace all occurrences |
| 254 | if($limit === 0 && $offset === 0){ |
| 255 | return str_replace($search, $replace, $subject); |
| 256 | } |
| 257 | |
| 258 | // count total occurrences in original subject |
| 259 | $occurrences = substr_count($subject, $search); |
| 260 | if($occurrences === 0){ |
| 261 | return $subject; |
| 262 | } |
| 263 | |
| 264 | // negative offset, start from the end |
| 265 | if($offset < 0){ |
| 266 | $offset = $occurrences + $offset; |
| 267 | } |
| 268 | |
| 269 | // out of range offset |
| 270 | if($offset < 0 || $offset >= $occurrences){ |
| 271 | return $subject; |
| 272 | } |
| 273 | |
| 274 | // replacement boundaries based on occurrence index |
| 275 | $replace_from = $offset; |
| 276 | $replace_to = $limit === 0 ? $occurrences : min($occurrences, $offset + $limit); // exclusive |
| 277 | |
| 278 | // iterate on original subject and rebuild output safely |
| 279 | $result = ''; |
| 280 | $search_len = strlen($search); |
| 281 | $scan_offset = 0; |
| 282 | $chunk_start = 0; |
| 283 | $occurrence_index = 0; |
| 284 | |
| 285 | // loop through occurrences of search string |
| 286 | while(($pos = strpos($subject, $search, $scan_offset)) !== false){ |
| 287 | |
| 288 | // append everything before the current occurrence |
| 289 | $result .= substr($subject, $chunk_start, $pos - $chunk_start); |
| 290 | |
| 291 | // before replacement window: keep search |
| 292 | if($occurrence_index < $replace_from){ |
| 293 | $result .= $search; |
| 294 | |
| 295 | // inside replacement window: replace |
| 296 | }elseif($occurrence_index < $replace_to){ |
| 297 | $result .= $replace; |
| 298 | |
| 299 | // after replacement window |
| 300 | }else{ |
| 301 | if(!$delete_others){ |
| 302 | $result .= $search; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | $occurrence_index++; |
| 307 | $scan_offset = $pos + $search_len; |
| 308 | $chunk_start = $scan_offset; |
| 309 | } |
| 310 | |
| 311 | // append remaining tail |
| 312 | $result .= substr($subject, $chunk_start); |
| 313 | |
| 314 | // return |
| 315 | return $result; |
| 316 | |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /** |
| 321 | * acfe_str_replace_first |
| 322 | * |
| 323 | * @param $search |
| 324 | * @param $replace |
| 325 | * @param $subject |
| 326 | * @param $delete_others bool Should delete the other occurrences of the search string |
| 327 | * |
| 328 | * @deprecated since 0.9.2.6 |
| 329 | * |
| 330 | * @return array|mixed|string|string[] |
| 331 | */ |
| 332 | function acfe_str_replace_first($search, $replace, $subject, $delete_others = false){ |
| 333 | acfe_deprecated_function('acfe_str_replace_first()', '0.9.2.6', 'Use acfe_str_replace() with offset = 0 & limit = 1'); |
| 334 | return acfe_str_replace($subject, $search, $replace, 0, 1, $delete_others); |
| 335 | } |