|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ReliQArts\Mardin\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use tidy; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
|
|
8
|
|
|
class StringHelper |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Capatilize first letter of each word of a string. |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $value |
|
14
|
|
|
* @return string |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function title($value) |
|
17
|
|
|
{ |
|
18
|
|
|
return mb_convert_case($value, MB_CASE_TITLE); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param $value |
|
23
|
|
|
* @internal param $html |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function tidy($value) |
|
27
|
|
|
{ |
|
28
|
|
|
// Check to see if Tidy is available. |
|
29
|
|
|
if (class_exists('tidy')) { |
|
30
|
|
|
$tidy = new tidy(); |
|
31
|
|
|
|
|
32
|
|
|
return $tidy->repairString($value, [ |
|
33
|
|
|
'show-body-only' => true, |
|
34
|
|
|
]); |
|
35
|
|
|
} else { // No Tidy, Time for regex and possibly a broken DOM :( |
|
36
|
|
|
preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $value, $result); |
|
37
|
|
|
$openedtags = $result[1]; |
|
38
|
|
|
preg_match_all('#</([a-z]+)>#iU', $value, $result); |
|
39
|
|
|
$closedtags = $result[1]; |
|
40
|
|
|
$len_opened = count($openedtags); |
|
41
|
|
|
if (count($closedtags) == $len_opened) { |
|
42
|
|
|
return $value; |
|
43
|
|
|
} |
|
44
|
|
|
$openedtags = array_reverse($openedtags); |
|
45
|
|
|
for ($i = 0; $i < $len_opened; $i++) { |
|
46
|
|
|
if (! in_array($openedtags[$i], $closedtags)) { |
|
47
|
|
|
$value .= '</'.$openedtags[$i].'>'; |
|
48
|
|
|
} else { |
|
49
|
|
|
unset($closedtags[array_search($openedtags[$i], $closedtags)]); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $value; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function date(Carbon $date) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($date->diffInDays(Carbon::now()) < 7) { |
|
60
|
|
|
return $date->diffForHumans(); |
|
61
|
|
|
} else { |
|
62
|
|
|
return $date->toFormattedDateString(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function firstNWords($content = null, $wordsreturned = 20, $suffix = ' ...') |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
//return ""; |
|
69
|
|
|
$string = ''; |
|
70
|
|
|
if ($content) { |
|
71
|
|
|
$string = $content; |
|
72
|
|
|
} |
|
73
|
|
|
/* Returns the first $wordsreturned out of $string. If string |
|
74
|
|
|
contains fewer words than $wordsreturned, the entire string |
|
75
|
|
|
is returned. |
|
76
|
|
|
*/ |
|
77
|
|
|
|
|
78
|
|
|
$retval = $string; // Just in case of a problem |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$array = explode(' ', $string); |
|
81
|
|
|
|
|
82
|
|
|
if (count($array) <= $wordsreturned) { |
|
83
|
|
|
/* Already short enough, return the whole thing |
|
84
|
|
|
*/ |
|
85
|
|
|
$retval = $string; |
|
86
|
|
|
} else { |
|
87
|
|
|
/* Need to chop of some words |
|
88
|
|
|
*/ |
|
89
|
|
|
array_splice($array, $wordsreturned); |
|
90
|
|
|
$retval = implode(' ', $array); |
|
91
|
|
|
$retval .= $suffix; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $retval; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.