-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
88 lines (76 loc) · 2.56 KB
/
index.php
File metadata and controls
88 lines (76 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* Uncomment this for use with Wolf 0.7.0 and above
* Security measure for Wolf 0.7.0
*/
if (!defined('IN_CMS')) { exit(); }
/**
* This plugin allows you to automate article excerpts by number of characters.
*
* @package plugins
* @subpackage truncate
*
* @author Dejan Andjelkovic <dejan79@gmail.com>
* @version 0.0.2
* @since Wolf version 0.6.0
* @license http://www.gnu.org/licenses/gpl.html GPL License
* @copyright Dejan Andjelkovic, 2011+
*/
Plugin::setInfos(array(
'id' => 'truncate',
'title' => __('Truncate'),
'description' => __('Automated excerpts'),
'version' => '0.0.2',
'license' => 'GPL',
'author' => 'Dejan Andjelkovic',
'website' => 'http://project79.net/',
'update_url' => 'http://project79.net/plugin-versions.xml',
'require_wolf_version' => '0.6.0'
));
/*
* Example:
* $myarticles = truncate($article->content,300);
* echo $myarticles;
*/
function truncate($text, $length, $suffix = '…', $isHTML = true){
$i = 0;
$simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
$tags = array();
if($isHTML){
preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach($m as $o){
if($o[0][1] - $i >= $length)
break;
$t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
// test if the tag is unpaired, then we mustn't save them
if($t[0] != '/' && (!isset($simpleTags[$t])))
$tags[] = $t;
elseif(end($tags) == substr($t, 1))
array_pop($tags);
$i += $o[1][1] - $o[0][1];
}
}
// output without closing tags
$output = substr($text, 0, $length = min(strlen($text), $length + $i));
// closing tags
$output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');
// Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
$postemp = preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE);
$postemp2 = end($postemp);
$pos = (int)end($postemp2);
// Append closing tags to output
$output.=$output2;
// Get everything until last space
$one = substr($output, 0, $pos);
// Get the rest
$two = substr($output, $pos, (strlen($output) - $pos));
// Extract all tags from the last bit
preg_match_all('/<(.*?)>/s', $two, $tags);
// Add suffix if needed
if (strlen($text) > $length) { $one .= $suffix; }
// Re-attach tags
$output = $one . implode($tags[0]);
//added to remove unnecessary closure
$output = str_replace('</!-->','',$output);
return $output;
}