reasonableframework/helper/string.utl.php

165 lines
4.2 KiB
PHP
Raw Normal View History

2018-05-15 18:19:27 +00:00
<?php
2018-05-26 16:11:32 +00:00
/**
* @file string.utl.php
* @date 2018-05-27
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief String utility helper
*/
2018-09-09 16:30:13 +00:00
// for Korean Telephone Number
2019-02-26 05:40:03 +00:00
if(!check_function_exists("parse_tel_number_kr")) {
2018-09-09 16:30:13 +00:00
function parse_tel_number_kr($tel) {
2018-09-09 16:40:30 +00:00
$output = preg_replace("/[^0-9]/", "", $tel); // 숫자 이외 제거
2018-09-09 16:41:36 +00:00
$local_code = substr($tel, 0, 2);
2018-09-09 16:40:30 +00:00
2018-09-09 16:41:36 +00:00
if ($local_code == '02') {
2018-09-09 16:40:30 +00:00
$output = preg_replace("/([0-9]{2})([0-9]{3,4})([0-9]{4})$/", "\\1-\\2-\\3", $tel);
2018-09-09 16:41:36 +00:00
} elseif (strlen($tel) == '8' && in_array($local_code, array('15', '16', '18'))) {
$output = preg_replace("/([0-9]{4})([0-9]{4})$/", "\\1-\\2", $tel); // 지능망 번호이면
2018-09-09 16:40:30 +00:00
} else {
$output = preg_replace("/([0-9]{3})([0-9]{3,4})([0-9]{4})$/", "\\1-\\2-\\3", $tel);
}
return $output;
2018-05-26 16:11:32 +00:00
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_converted_string")) {
2018-09-09 17:01:12 +00:00
function get_converted_string($str, $to_charset, $from_charset) {
2018-09-09 16:48:57 +00:00
$result = false;
2018-09-09 17:01:12 +00:00
if($form_charset == "detect") {
2019-02-26 06:06:57 +00:00
if(check_function_exists(array("mb_detect_encoding", "mb_detect_order"))) {
2018-09-09 17:01:12 +00:00
$from_charset = mb_detect_encoding($str, mb_detect_order(), true);
} else {
$from_charset = "ISO-8859-1";
}
2018-09-09 16:37:11 +00:00
}
2019-02-26 06:04:05 +00:00
if(check_function_exists("iconv")) {
2018-09-09 17:01:12 +00:00
$result = iconv($from_charset, $to_charset, $str);
2019-02-26 06:04:05 +00:00
} elseif(check_function_exists("mb_convert_encoding")) {
2018-09-09 17:01:12 +00:00
$result = mb_convert_encoding($str, $to_charset, $from_charset);
2018-09-09 16:48:57 +00:00
}
2018-09-09 17:01:12 +00:00
return $result;
2018-09-09 16:37:11 +00:00
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("nl2p")) {
2018-05-26 16:11:32 +00:00
function nl2p($string) {
$paragraphs = '';
foreach (explode("\n", $string) as $line) {
if (trim($line)) {
$paragraphs .= '<p>' . $line . '</p>';
}
}
return $paragraphs;
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("br2nl")) {
2018-05-26 16:11:32 +00:00
function br2nl($string) {
return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("br2p")) {
2018-05-26 16:11:32 +00:00
function br2p($string) {
return nl2p(br2nl($string));
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_formatted_number")) {
2018-05-26 16:11:32 +00:00
function get_formatted_number($value) {
return number_format(floatval($value));
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_cutted_string")) {
2018-05-26 16:11:32 +00:00
function get_cutted_string($str, $start, $len=0, $charset="utf-8") {
$result = "";
2019-02-26 06:04:05 +00:00
if(check_function_exists("iconv_substr")) {
2018-05-26 16:11:32 +00:00
$result = iconv_substr($str, $start, $len, $charset);
2019-02-26 06:04:05 +00:00
} elseif(check_function_exists("mb_substr")) {
2018-05-26 16:11:32 +00:00
$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-02-26 06:38:05 +00:00
if(!check_function_exists("split_by_line")) {
function split_by_line($str) {
2018-05-26 17:02:57 +00:00
return preg_split('/\n|\r\n?/', $str);
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("read_storage_file_by_line")) {
2018-05-26 17:02:57 +00:00
function read_storage_file_by_line($filename, $options=array()) {
2018-05-26 17:04:05 +00:00
return explode_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")) {
2018-09-26 16:59:17 +00:00
function startsWith($haystack, $needle) {
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("endsWith")) {
2018-09-26 16:59:17 +00:00
function endsWith($haystack, $needle) {
$length = strlen($needle);
if($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
}
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")) {
function multi_explode($delimiters, $string) {
2018-09-28 06:29:13 +00:00
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch;
}
}
2018-10-28 08:57:31 +00:00
2019-02-26 05:40:03 +00:00
if(!check_function_exists("parse_pipelined_data")) {
2018-10-28 08:57:31 +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;
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("eregi_compatible")) {
function eregi_compatible($pattern, $subject, &$matches=NULL) {
return preg_match(sprintf("/%s/i", $pattern), $subject, $matches);
}
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("eregi_replace_compatible")) {
function eregi_replace_compatible($pattern, $replacement, $subject) {
return preg_replace(sprintf("/%s/i", $pattern), $replacement, $subject);
}
}