2018-05-15 18:19:27 +00:00
|
|
|
<?php
|
2018-05-26 16:11:32 +00:00
|
|
|
/**
|
2019-02-26 07:03:12 +00:00
|
|
|
* @file string.utils.php
|
2018-05-26 16:11:32 +00:00
|
|
|
* @date 2018-05-27
|
|
|
|
* @author Go Namhyeon <gnh1201@gmail.com>
|
|
|
|
* @brief String utility helper
|
|
|
|
*/
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("get_converted_string")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function get_converted_string($str, $to_charset, $from_charset) {
|
|
|
|
$result = false;
|
|
|
|
|
|
|
|
if($form_charset == "detect") {
|
|
|
|
if(check_function_exists(array("mb_detect_encoding", "mb_detect_order"))) {
|
|
|
|
$from_charset = mb_detect_encoding($str, mb_detect_order(), true);
|
|
|
|
} else {
|
|
|
|
$from_charset = "ISO-8859-1";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(check_function_exists("iconv")) {
|
|
|
|
$result = iconv($from_charset, $to_charset, $str);
|
|
|
|
} elseif(check_function_exists("mb_convert_encoding")) {
|
|
|
|
$result = mb_convert_encoding($str, $to_charset, $from_charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2018-09-09 16:37:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("nl2p")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function nl2p($string) {
|
|
|
|
$paragraphs = '';
|
|
|
|
foreach (explode("\n", $string) as $line) {
|
|
|
|
if (trim($line)) {
|
|
|
|
$paragraphs .= '<p>' . $line . '</p>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $paragraphs;
|
|
|
|
}
|
2018-05-26 16:11:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("br2nl")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function br2nl($string) {
|
|
|
|
return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
|
|
|
|
}
|
2018-05-26 16:11:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("br2p")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function br2p($string) {
|
|
|
|
return nl2p(br2nl($string));
|
|
|
|
}
|
2018-05-26 16:11:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("get_formatted_number")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function get_formatted_number($value) {
|
|
|
|
return number_format(floatval($value));
|
|
|
|
}
|
2018-05-26 16:11:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("get_cutted_string")) {
|
2019-09-11 02:38:34 +00:00
|
|
|
function get_cutted_string($str, $start, $len=null, $charset="utf-8") {
|
2019-05-20 08:19:05 +00:00
|
|
|
$result = "";
|
|
|
|
|
|
|
|
if(check_function_exists("iconv_substr")) {
|
|
|
|
$result = iconv_substr($str, $start, $len, $charset);
|
|
|
|
} elseif(check_function_exists("mb_substr")) {
|
|
|
|
$result = mb_substr($str, $start, $len, $charset);
|
|
|
|
} else {
|
|
|
|
$result = substr($str, $start, $len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2018-05-15 18:19:27 +00:00
|
|
|
}
|
2018-05-26 17:02:57 +00:00
|
|
|
|
2019-09-11 02:40:01 +00:00
|
|
|
if(!check_function_exists("get_string_length")) {
|
2019-09-11 02:38:34 +00:00
|
|
|
function get_string_length($str, $charset="utf-8") {
|
|
|
|
$len = 0;
|
|
|
|
|
|
|
|
if(check_function_exists("iconv_strlen")) {
|
|
|
|
$len = iconv_strlen($str, $charset);
|
|
|
|
} elseif(check_function_exists("mb_strlen")) {
|
|
|
|
$len = mb_strlen($str, $charset);
|
|
|
|
} else {
|
|
|
|
$len = strlen($str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!check_function_exists("get_splitted_strings")) {
|
|
|
|
function get_splitted_strings($str, $len=32, $chsarset="utf-8") {
|
|
|
|
$strings = array();
|
|
|
|
|
|
|
|
$_len = get_string_length($str);
|
|
|
|
$_pos = 0;
|
2019-09-11 02:58:56 +00:00
|
|
|
if($len >= $_len) {
|
|
|
|
$strings[] = $str;
|
|
|
|
} else {
|
|
|
|
$__len = ceil($_len / $len);
|
|
|
|
for($i = 0; $i < $__len; $i++) {
|
|
|
|
$_pos = $len * $i;
|
|
|
|
$strings[] = get_cutted_string($str, $_pos, $len, $charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($_len - $_pos > 0) {
|
|
|
|
$strings[] = $strings[] = get_cutted_string($str, $_pos);
|
2019-09-11 02:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $strings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 06:38:05 +00:00
|
|
|
if(!check_function_exists("split_by_line")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function split_by_line($str) {
|
|
|
|
return preg_split('/\n|\r\n?/', $str);
|
|
|
|
}
|
2018-05-26 17:02:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("read_storage_file_by_line")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function read_storage_file_by_line($filename, $options=array()) {
|
|
|
|
return split_by_line(read_storage_file($filename, $options));
|
|
|
|
}
|
2018-05-26 17:02:57 +00:00
|
|
|
}
|
2018-09-26 16:59:17 +00:00
|
|
|
|
2018-09-28 06:30:24 +00:00
|
|
|
// https://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("startsWith")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function startsWith($haystack, $needle) {
|
|
|
|
$length = strlen($needle);
|
|
|
|
return (substr($haystack, 0, $length) === $needle);
|
|
|
|
}
|
2018-09-26 16:59:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("endsWith")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function endsWith($haystack, $needle) {
|
|
|
|
$length = strlen($needle);
|
|
|
|
if($length == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-26 16:59:17 +00:00
|
|
|
|
2019-05-20 08:19:05 +00:00
|
|
|
return (substr($haystack, -$length) === $needle);
|
|
|
|
}
|
2018-09-26 16:59:17 +00:00
|
|
|
}
|
2018-09-28 06:29:13 +00:00
|
|
|
|
2018-09-28 06:30:24 +00:00
|
|
|
// https://stackoverflow.com/questions/4955433/php-multiple-delimiters-in-explode/27767665#27767665
|
2019-02-26 06:38:05 +00:00
|
|
|
if(!check_function_exists("multi_explode")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function multi_explode($delimiters, $string) {
|
|
|
|
$ready = str_replace($delimiters, $delimiters[0], $string);
|
|
|
|
$launch = explode($delimiters[0], $ready);
|
|
|
|
return $launch;
|
|
|
|
}
|
2018-09-28 06:29:13 +00:00
|
|
|
}
|
2018-10-28 08:57:31 +00:00
|
|
|
|
2019-02-26 08:35:31 +00:00
|
|
|
if(!check_function_exists("multi_strpos")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function multi_strpos($string, $delimiters, $offset=0) {
|
|
|
|
$last_pos = strlen($string) - 1;
|
|
|
|
$pos = $last_pos;
|
|
|
|
|
|
|
|
if($offset > 0) {
|
|
|
|
$string = substr($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($delimiters as $s) {
|
|
|
|
$new_pos = strpos($string, $s);
|
|
|
|
if($new_pos !== false && $pos > $new_pos) {
|
|
|
|
$pos = $new_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (($pos < $last_pos) ? $pos : false);
|
|
|
|
}
|
2019-02-26 08:35:31 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 09:06:19 +00:00
|
|
|
if(!check_function_exists("multi_str_split")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function multi_str_split($string, $delimiters) {
|
|
|
|
$strings = array();
|
|
|
|
|
|
|
|
if(is_string($string)) {
|
|
|
|
$offset = 0;
|
|
|
|
$pos = -1;
|
|
|
|
while(!($pos !== false)) {
|
|
|
|
$offset = $pos + 1;
|
|
|
|
$pos = multi_strpos($string, $delimiters, $offset);
|
|
|
|
$strings[] = substr($string, $offset, $pos - $offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $strings;
|
|
|
|
}
|
2019-02-26 09:06:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("parse_pipelined_data")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function parse_pipelined_data($pipelined_data, $keynames=array()) {
|
|
|
|
$result = array();
|
|
|
|
$parsed_data = explode("|", $pipelined_data);
|
|
|
|
|
|
|
|
if(count($keynames) > 0) {
|
|
|
|
$i = 0;
|
|
|
|
foreach($keynames as $name) {
|
|
|
|
$result[$name] = $parsed_data[$i];
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = $parsed_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2018-10-28 08:57:31 +00:00
|
|
|
}
|
2018-12-29 03:23:14 +00:00
|
|
|
|
2019-09-26 08:05:03 +00:00
|
|
|
// https://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences
|
|
|
|
if(!check_function_exists("remove_utf8_bom")) {
|
|
|
|
function remove_utf8_bom($text) {
|
|
|
|
$bom = pack('H*','EFBBBF');
|
|
|
|
$text = preg_replace("/^$bom/", '', $text);
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 10:48:34 +00:00
|
|
|
if(!check_function_exists("get_tokenized_text")) {
|
2019-09-30 02:01:48 +00:00
|
|
|
function get_tokenized_text($text, $delimiters=array(",", " ", "|", "-", "+","\t","\r\n","\n")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
return array_filter(multi_explode($delimiters, $text));
|
|
|
|
}
|
2019-05-03 10:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!check_function_exists("get_highlighted_html_by_words")) {
|
2019-09-30 02:01:48 +00:00
|
|
|
function get_highlighted_html_by_word($word, $text, $delimiters=array(",", " ", "|", "-", "+","\t","\r\n","\n")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
$html = $text;
|
2019-05-03 10:48:34 +00:00
|
|
|
|
2019-05-20 08:19:05 +00:00
|
|
|
$words = get_tokenized_text($word, $delimiters);
|
|
|
|
if(check_array_length($words, 0) > 0) {
|
|
|
|
$html = preg_replace(sprintf("/%s/i", implode("|", $words)), "<strong class=\"highlight\">$0</strong>", $text);
|
|
|
|
}
|
2019-05-03 10:48:34 +00:00
|
|
|
|
2019-05-20 08:19:05 +00:00
|
|
|
return $html;
|
2019-05-03 05:41:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 02:53:48 +00:00
|
|
|
if(!check_function_exists("get_floating_percentage")) {
|
|
|
|
function get_floating_percentage($x, $a=2) {
|
2019-09-30 02:03:48 +00:00
|
|
|
return round(floatval($x) / 100, floatval($a));
|
2019-06-13 02:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("eregi_compatible")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function eregi_compatible($pattern, $subject, &$matches=NULL) {
|
|
|
|
return preg_match(sprintf("/%s/i", $pattern), $subject, $matches);
|
|
|
|
}
|
2018-12-29 03:23:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 05:40:03 +00:00
|
|
|
if(!check_function_exists("eregi_replace_compatible")) {
|
2019-05-20 08:19:05 +00:00
|
|
|
function eregi_replace_compatible($pattern, $replacement, $subject) {
|
|
|
|
return preg_replace(sprintf("/%s/i", $pattern), $replacement, $subject);
|
|
|
|
}
|
2018-12-29 03:23:14 +00:00
|
|
|
}
|