| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- /**
- * Text helper file.
- *
- * Provides a helper to perform text operations.
- *
- * @author Iñigo Valentin <i@inigovalentin.com>
- * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
- * @package SWDB
- */
- require_once(PATH::HELPER . "HELPER.php");
- /**
- * Text helper.
- *
- * Contains utilities to format and escapee text.
- *
- * @category Helper.
- */
- final class TEXT extends Helper{
- /**
- * Turns a date string into a human readable string on the specified
- * language. Only works for spanish, basque and english.
- *
- * @param string $str_date Date string ('yyyy-mm-dd' or 'yyyy-mm-dd HH:MM:SS')
- * @param string $time Append the time at the end.
- * @return string Formatted date string.
- */
- public static function format_date($str_date, $time = true){
- $date = strtotime($str_date);
- $year = date("o", $date);
- $month = date("n", $date);
- $month --;
- $day = date("j", $date);
- $wday = date("N", $date);
- $wday --;
- $hour = date("H", $date);
- $minute = date("i", $date);
- $week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
- $months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
- if ($time){
- $str = "$week[$wday], $months[$month] $day, $year at $hour:$minute";
- }
- else{
- $str = "$week[$wday], $months[$month] $day, $year";
- }
- return $str;
- }
- /**
- * Closes all the opened HTML tags in a given string.
- *
- * @param string html The string with HTML tags.
- * @return string HTML with closed tags.
- */
- public static function close_tags($html) {
- $result = [];
- preg_match_all("#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU", $html, $result);
- $openedtags = $result[1];
- preg_match_all("#</([a-z]+)>#iU", $html, $result);
- $closedtags = $result[1];
- $len_opened = count($openedtags);
- if (count($closedtags) == $len_opened) {
- return $html;
- }
- $openedtags = array_reverse($openedtags);
- for ($i=0; $i < $len_opened; $i++) {
- if (!in_array($openedtags[$i], $closedtags)) {
- $html .= "</".$openedtags[$i].">";
- } else {
- unset($closedtags[array_search($openedtags[$i], $closedtags)]);
- }
- }
- return $html;
- }
- /**
- * Text shortener.
- *
- * Given a string, it trims in the proximity of the
- * desired string, up to the next white character. If indicated, it will
- * append a link to the full text.
- *
- * @param string $text The text to shorten.
- * @param int $length The desired length.
- * @param string $link_text Text for the link. Optional.
- * @param string $link URI of the link.
- * @return string Shortened text.
- */
- public static function cut_text($text, $length, $link_text = "", $link = ""){
- if (strlen($text) < $length){
- return $text;
- }
- $cut = substr($text, 0, strpos($text, " ", $length));
- $cut = close_tags($cut);
- if (strlen($cut) == 0){
- $cut = $text;
- }
- if (strlen($text) != strlen($cut) && strlen($link) > 0 && strlen($link_text) > 0){
- $cut = $cut . "... <a href='$link'>$link_text</a>";
- }
- return $cut;
- }
- }
|