forked from fossar/selfoss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewHelper.php
More file actions
72 lines (60 loc) · 2.2 KB
/
ViewHelper.php
File metadata and controls
72 lines (60 loc) · 2.2 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
<?PHP
namespace helpers;
/**
* Helper class for loading extern items
*
* @package helpers
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
* @author Tobias Zeising <tobias.zeising@aditu.de>
*/
class ViewHelper {
/** encloses all searchWords with <span class=found>$word</span>
* for later highlitning with CSS
*
* @return string with highlited words
* @param string $content which contains words
* @param array|string $searchWords words for highlighting
*/
public function highlight($content, $searchWords) {
if(strlen(trim($searchWords))==0)
return $content;
if(!is_array($searchWords))
$searchWords = explode(" ", $searchWords);
foreach($searchWords as $word)
$content = preg_replace('/(?!<[^<>])('.$word.')(?![^<>]*>)/i','<span class=found>$0</span>',$content);
return $content;
}
/**
* removes img src attribute and saves the value in ref for
* loading it later
*
* @return string with replaced img tags
* @param string $content which contains img tags
*/
public function lazyimg($content) {
return preg_replace("/<img([^<]+)src=(['\"])([^\"']*)(['\"])([^<]*)>/i","<img$1ref='$3'$5>",$content);
}
/**
* format given date as "x days ago"
*
* @return string with replaced formateddate
* @param
*/
public function dateago($datestr) {
$date = new \DateTime($datestr);
$now = new \DateTime();
$ageInSeconds = $now->getTimestamp() - $date->getTimestamp();
$ageInMinutes = $ageInSeconds / 60;
$ageInHours = $ageInMinutes / 60;
$ageInDays = $ageInHours / 24;
if($ageInMinutes<1)
return \F3::get('lang_seconds',round($ageInSeconds, 0));
if($ageInHours<1)
return \F3::get('lang_minutes',round($ageInMinutes, 0));
if($ageInDays<1)
return \F3::get('lang_hours',round($ageInHours, 0));
//return $datestr;
return \F3::get('lang_timestamp', $date->getTimestamp());
}
}