PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.32
E2Pdf – Export Pdf Tool for WordPress v1.32.32
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / helper / e2pdf-truncate.php

e2pdf-truncate.php in E2Pdf – Export Pdf Tool for WordPress 1.32.32, at classes/helper/e2pdf-truncate.php

102 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * E2Pdf Truncate Helper
5 * @copyright Copyright 2017 https://e2pdf.com
6 * @license GPLv3
7 * @version 1
8 * @link https://e2pdf.com
9 * @since 1.01.02
10 */
11 if (!defined('ABSPATH')) {
12 die('Access denied.');
13 }
14
15 class Helper_E2pdf_Truncate {
16
17 private $helper;
18 private $max_length = 0;
19 private $read_more = '';
20 private $break_words = false;
21 private $length = 0;
22 private $limit = false;
23 private $elements = array();
24
25 public function __construct() {
26 $this->helper = Helper_E2pdf_Helper::instance();
27 }
28
29 public function truncate($text = '', $max_length = 100, $read_more = '', $break_words = false, $is_html = false) {
30 if (!$text || mb_strlen($text) < $max_length) {
31 return $text;
32 }
33 if ($is_html) {
34 $this->length = 0;
35 $this->limit = false;
36 $this->elements = array();
37 $this->max_length = $max_length;
38 $this->read_more = $read_more;
39 $this->break_words = $break_words;
40 $text = '<div>' . $text . '</div>';
41 $dom = new DOMDocument();
42 $html = $this->helper->load('convert')->load_html($text, $dom);
43 if ($html) {
44 $remove = $this->truncate_html($dom);
45 foreach ($remove as $child) {
46 $child->parentNode->removeChild($child);
47 }
48 $container = $dom->getElementsByTagName('div')->item(0);
49 $container = $container->parentNode->removeChild($container);
50 while ($dom->firstChild) {
51 $dom->removeChild($dom->firstChild);
52 }
53 while ($container->firstChild) {
54 $dom->appendChild($container->firstChild);
55 }
56 $text = $this->helper->load('convert')->html_entities_decode($dom->saveHTML());
57 }
58 } else {
59 if (!$break_words) {
60 $text = rtrim($text) . ' ';
61 }
62 $text = mb_substr($text, 0, $max_length);
63 if (!$break_words) {
64 $text = mb_substr($text, 0, strrpos($text, ' '));
65 }
66 $text = rtrim($text) . $read_more;
67 }
68 return $text;
69 }
70
71 public function truncate_html($node) {
72 if ($this->limit) {
73 $this->elements[] = $node;
74 } else {
75 if ($node && $node instanceof DOMText) {
76 $nodeLen = mb_strlen($node->nodeValue);
77 $this->length += $nodeLen;
78 if ($this->length > $this->max_length) {
79 if (!$this->break_words) {
80 $text = rtrim($node->nodeValue) . ' ';
81 } else {
82 $text = $node->nodeValue;
83 }
84 $text = mb_substr($text, 0, $nodeLen - ($this->length - $this->max_length));
85 if (!$this->break_words) {
86 $text = mb_substr($text, 0, strrpos($text, ' '));
87 }
88 $text = rtrim($text) . $this->read_more;
89 $node->nodeValue = $text;
90 $this->limit = true;
91 }
92 }
93 if ($node->hasChildNodes()) {
94 foreach ($node->childNodes as $child) {
95 $this->truncate_html($child);
96 }
97 }
98 }
99 return $this->elements;
100 }
101 }
102