Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Text.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan;
 12: 
 13: /**
 14:  * String functions
 15:  *
 16:  * @author Goragod Wiriya <admin@goragod.com>
 17:  *
 18:  * @since 1.0
 19:  */
 20: class Text
 21: {
 22:     /**
 23:      * ฟังก์ชั่น ตัดสตริงค์ตามความยาวที่กำหนด
 24:      * หากข้อความที่นำมาตัดยาวกว่าที่กำหนด จะตัดข้อความที่เกินออก และเติม .. ข้างท้าย
 25:      *
 26:      * @assert ('สวัสดี ประเทศไทย', 8) [==] 'สวัสดี..'
 27:      * @assert ('123456789', 8) [==] '123456..'
 28:      *
 29:      * @param string $source ข้อความ
 30:      * @param int    $len    ความยาวของข้อความที่ต้องการ (จำนวนตัวอักษรรวมจุด)
 31:      *
 32:      * @return string
 33:      */
 34:     public static function cut($source, $len)
 35:     {
 36:         if (!empty($len)) {
 37:             $len = (int) $len;
 38:             $source = (mb_strlen($source) <= $len || $len < 3) ? $source : mb_substr($source, 0, $len - 2).'..';
 39:         }
 40: 
 41:         return $source;
 42:     }
 43: 
 44:     /**
 45:      * ฟังก์ชั่น แปลงขนาดของไฟล์จาก byte เป็น kb mb
 46:      * คืนค่าขนาดของไฟล์เป็น KB MB
 47:      *
 48:      * @assert (256) [==] '256 Bytes'
 49:      * @assert (1024) [==] '1 KB'
 50:      * @assert (1024 * 1024) [==] '1 MB'
 51:      * @assert (1024 * 1024 * 1024) [==] '1 GB'
 52:      * @assert (1024 * 1024 * 1024 * 1024) [==] '1 TB'
 53:      *
 54:      * @param int $bytes     ขนาดของไฟล์ เป็น byte
 55:      * @param int $precision จำนวนหลักหลังจุดทศนิยม (default 2)
 56:      *
 57:      * @return string
 58:      */
 59:     public static function formatFileSize($bytes, $precision = 2)
 60:     {
 61:         $units = array('Bytes', 'KB', 'MB', 'GB', 'TB');
 62:         if ($bytes <= 0) {
 63:             return '0 Byte';
 64:         } else {
 65:             $bytes = max($bytes, 0);
 66:             $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
 67:             $pow = min($pow, count($units) - 1);
 68:             $bytes /= pow(1024, $pow);
 69: 
 70:             return round($bytes, $precision).' '.$units[$pow];
 71:         }
 72:     }
 73: 
 74:     /**
 75:      * ฟังก์ชั่น HTML highlighter
 76:      * แปลง BBCode
 77:      * แปลงข้อความ http เป็นลิงค์
 78:      * คืนค่าข้อความ
 79:      *
 80:      * @param string $detail ข้อความ
 81:      *
 82:      * @return string
 83:      */
 84:     public static function highlighter($detail)
 85:     {
 86:         $detail = preg_replace_callback('/\[([uo]l)\](.*)\[\/\\1\]/is', function ($match) {
 87:             return '<'.$match[1].'><li>'.preg_replace('/<br(\s\/)?>/is', '</li><li>', $match[2]).'</li></'.$match[1].'>';
 88:         }, $detail);
 89:         $patt[] = '/\[(i|dfn|b|strong|u|em|ins|del|sub|sup|small|big)\](.*)\[\/\\1\]/is';
 90:         $replace[] = '<\\1>\\2</\\1>';
 91:         $patt[] = '/\[color=([#a-z0-9]+)\]/i';
 92:         $replace[] = '<span style="color:\\1">';
 93:         $patt[] = '/\[size=([0-9]+)(px|pt|em|\%)\]/i';
 94:         $replace[] = '<span style="font-size:\\1\\2">';
 95:         $patt[] = '/\[\/(color|size)\]/i';
 96:         $replace[] = '</span>';
 97:         $patt[] = '/\[url\](.*)\[\/url\]/i';
 98:         $replace[] = '<a href="\\1" target="_blank">\\1</a>';
 99:         $patt[] = '/\[url=(ftp|https?):\/\/(.*)\](.*)\[\/url\]/i';
100:         $replace[] = '<a href="\\1://\\2" target="_blank">\\3</a>';
101:         $patt[] = '/\[url=(\/)?(.*)\](.*)\[\/url\]/i';
102:         $replace[] = '<a href="'.WEB_URL.'\\2" target="_blank">\\3</a>';
103:         $patt[] = '/([^["]]|\r|\n|\s|\t|^)((ftp|https?):\/\/([a-z0-9\.\-_]+)\/([^\s<>\"\']{1,})([^\s<>\"\']{20,20}))/i';
104:         $replace[] = '\\1<a href="\\2" target="_blank">\\3://\\4/...\\6</a>';
105:         $patt[] = '/([^["]]|\r|\n|\s|\t|^)((ftp|https?):\/\/([^\s<>\"\']+))/i';
106:         $replace[] = '\\1<a href="\\2" target="_blank">\\2</a>';
107:         $patt[] = '/(<a[^>]+>)(https?:\/\/[^\%<]+)([\%][^\.\&<]+)([^<]{5,})(<\/a>)/i';
108:         $replace[] = '\\1\\2...\\4\\5';
109:         $patt[] = '/\[youtube\]([a-z0-9-_]+)\[\/youtube\]/i';
110:         $replace[] = '<div class="youtube"><iframe src="//www.youtube.com/embed/\\1?wmode=transparent"></iframe></div>';
111: 
112:         return preg_replace($patt, $replace, $detail);
113:     }
114: 
115:     /**
116:      * แปลง & " ' < > \ { } $ เป็น HTML entities ใช้แทน htmlspecialchars() ของ PHP
117:      *
118:      * @param string $text
119:      * @param bool $double_encode true (default) แปลง รหัส HTML เช่น &amp; เป็น &amp;amp;, false ไม่แปลง
120:      *
121:      * @return string
122:      */
123:     public static function htmlspecialchars($text, $double_encode = true)
124:     {
125:         $str = preg_replace(array('/&/', '/"/', "/'/", '/</', '/>/', '/\\\/', '/\{/', '/\}/', '/\$/'), array('&amp;', '&quot;', '&#039;', '&lt;', '&gt;', '&#92;', '&#x007B;', '&#x007D;', '&#36;'), $text);
126:         if (!$double_encode) {
127:             $str = preg_replace('/&(amp;([#a-z0-9]+));/i', '&\\2;', $str);
128:         }
129: 
130:         return $str;
131:     }
132: 
133:     /**
134:      * ฟังก์ชั่น ลบช่องว่าง และ ตัวอักษรขึ้นบรรทัดใหม่ ที่ติดกันเกินกว่า 1 ตัว
135:      * คืนค่าข้อความที่ไม่มีตัวอักษรขึ้นบรรทัดใหม่
136:      *
137:      * @assert (" \tทดสอบ\r\nภาษาไทย") [==] 'ทดสอบ ภาษาไทย'
138:      *
139:      * @param string $text ข้อความ
140:      * @param int    $len  จำนวนตัวอักษรสูงสุดที่ต้องการ, (default) คืนค่าทั้งหมด
141:      *
142:      * @return string
143:      */
144:     public static function oneLine($text, $len = 0)
145:     {
146:         return self::cut(trim(preg_replace('/[\r\n\t\s]+/', ' ', $text)), $len);
147:     }
148: 
149:     /**
150:      * รับค่าสำหรับ password อักขระทุกตัวไม่มีช่องว่าง
151:      *
152:      * @assert (" 0\n12   34\r\r6\t5ทดสอบ@#$&{}!?+_-=") [==] '0123465ทดสอบ@#$&{}!?+_-='
153:      *
154:      * @param string $text
155:      *
156:      * @return string
157:      */
158:     public static function password($text)
159:     {
160:         return preg_replace('/[^\w\@\#\$\&\{\}\!\?\+_\-=ก-ฮ]+/', '', $text);
161:     }
162: 
163:     /**
164:      * ลบตัวอักษรที่ไม่สามารถพิมพ์ได้ออก
165:      * ตั้งแต่ chr(128)-chr(255) หรือ \x80-\xFF ขึ้นไปจะถูกลบออก
166:      *
167:      * @assert (chr(0).chr(127).chr(128).chr(255)) [==] chr(0).chr(127)
168:      *
169:      * @param string $text
170:      *
171:      * @return string
172:      */
173:     public static function removeNonCharacters($text)
174:     {
175:         return preg_replace('/((?:[\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}){1,100})|./x', '\\1', $text);
176:     }
177: 
178:     /**
179:      * ฟังก์ชั่นคืนค่าข้อความซ้ำๆตามจำนวนที่กำหนด
180:      *
181:      * @assert ('0', 10) [==] '0000000000'
182:      *
183:      * @param string $text  ข้อความหรือตัวอักษรที่ต้องการทำซ้ำ
184:      * @param int    $count จำนวนที่ต้องการ
185:      *
186:      * @return string
187:      */
188:     public static function repeat($text, $count)
189:     {
190:         $result = '';
191:         for ($i = 0; $i < $count; ++$i) {
192:             $result .= $text;
193:         }
194: 
195:         return $result;
196:     }
197: 
198:     /**
199:      * แทนที่ข้อความด้วยข้อมูลจากแอเรย์ รองรับข้อมูลรูปแบบแอเรย์ย่อยๆ
200:      *
201:      * @assert ("SELECT * FROM table WHERE id=:id AND lang IN (:lang, '')", array(':id' => 1, array(':lang' => 'th'))) [==] "SELECT * FROM table WHERE id=1 AND lang IN (th, '')"
202:      *
203:      * @param string $source  ข้อความต้นฉบับ
204:      * @param array  $replace ข้อความที่จะนำมาแทนที่ รูปแบบ array($key1 => $value1, $key2 => $value2) ข้อความใน $source ที่ตรงกับ $key จะถูกแทนที่ด้วย $value
205:      *
206:      * @return string
207:      */
208:     public static function replace($source, $replace)
209:     {
210:         if (!empty($replace)) {
211:             $keys = array();
212:             $values = array();
213:             ArrayTool::extract($replace, $keys, $values);
214:             $source = str_replace($keys, $values, $source);
215:         }
216: 
217:         return $source;
218:     }
219: 
220:     /**
221:      * ฟังก์ชั่น เข้ารหัส อักขระพิเศษ และ {} ก่อนจะส่งให้กับ textarea หรือ editor ตอนแก้ไข
222:      * & " ' < > { } ไม่แปลง รหัส HTML เช่น &amp; &#38;.
223:      *
224:      * @assert ('&"'."'<>{}&amp;&#38;") [==] "&amp;&quot;&#039;&lt;&gt;&#x007B;&#x007D;&amp;&#38;"
225:      *
226:      * @param string $text ข้อความ
227:      *
228:      * @return string
229:      */
230:     public static function toEditor($text)
231:     {
232:         return preg_replace(array('/&/', '/"/', "/'/", '/</', '/>/', '/{/', '/}/', '/&(amp;([\#a-z0-9]+));/'), array('&amp;', '&quot;', '&#039;', '&lt;', '&gt;', '&#x007B;', '&#x007D;', '&\\2;'), $text);
233:     }
234: 
235:     /**
236:      * แปลง tag และ ลบช่องว่างไม่เกิน 1 ช่อง ไม่ขึ้นบรรทัดใหม่
237:      * เช่นหัวข้อของบทความ
238:      *
239:      * @assert (' ทด\/สอบ$'."\r\n\t".'<?php echo \'555\'?> ') [==] 'ทด&#92;/สอบ&#36; &lt;?php echo &#039;555&#039;?&gt;'
240:      * @assert ('&nbsp;') [==] '&amp;nbsp;'
241:      * @assert ('&nbsp;', false) [==] '&nbsp;'
242:      *
243:      * @param string $text
244:      * @param bool $double_encode true (default) แปลง รหัส HTML เช่น &amp; เป็น &amp;amp;, false ไม่แปลง
245:      *
246:      * @return string
247:      */
248:     public static function topic($text, $double_encode = true)
249:     {
250:         return trim(preg_replace('/[\r\n\s\t]+/', ' ', self::htmlspecialchars($text, $double_encode)));
251:     }
252: 
253:     /**
254:      * แปลง htmlspecialchars กลับเป็นอักขระปกติ
255:      *
256:      * @assert (\Kotchasan\Text::htmlspecialchars('&"\'<>\\{}$')) [==] '&"\'<>\\{}$'
257:      *
258:      * @param string $text
259:      *
260:      * @return string
261:      */
262:     public static function unhtmlspecialchars($text)
263:     {
264:         return str_replace(array('&amp;', '&quot;', '&#039;', '&lt;', '&gt;', '&#92;', '&#x007B;', '&#x007D;', '&#36;'), array('&', '"', "'", '<', '>', '\\', '{', '}', '$'), $text);
265:     }
266: 
267:     /**
268:      * แปลง tag ไม่แปลง &amp;
269:      * และลบช่องว่างหัวท้าย
270:      * สำหรับ URL หรือ email
271:      *
272:      * @assert (" http://www.kotchasan.com?a=1&b=2&amp;c=3 ") [==] 'http://www.kotchasan.com?a=1&amp;b=2&amp;c=3'
273:      * @assert ("javascript:alert('xxx')") [==] 'alertxxx'
274:      * @assert ("http://www.xxx.com/javascript/") [==] 'http://www.xxx.com/javascript/'
275:      *
276:      * @return string
277:      */
278:     public static function url($text)
279:     {
280:         $text = preg_replace('/(^javascript:|[\(\)\'\"]+)/', '', trim($text));
281: 
282:         return self::htmlspecialchars($text, false);
283:     }
284: 
285:     /**
286:      * ฟังก์ชั่นรับค่าสำหรับใช้เป็น username
287:      * รองรับอีเมล ตัวเลข (หมายเลขโทรศัพท์) @ - _ . เท่านั้น.
288:      *
289:      * @assert (' ad_min@demo.com') [==] 'ad_min@demo.com'
290:      * @assert ('012 3465') [==] '0123465'
291:      *
292:      * @param string $text
293:      *
294:      * @return string
295:      */
296:     public static function username($text)
297:     {
298:         return preg_replace('/[^a-zA-Z0-9@\.\-_]+/', '', $text);
299:     }
300: }
301: 
Kotchasan API documentation generated by ApiGen