1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Helper; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\Helper\Type\Str; |
6
|
|
|
|
7
|
|
|
class Text |
8
|
|
|
{ |
9
|
|
|
const WYSIWYG_BREAK_HTML = '<div style="page-break-after: always">'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Make snippet from text or html-text content. |
13
|
|
|
* @param string $text |
14
|
|
|
* @param int $length |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public static function snippet(string $text, int $length = 150) |
18
|
|
|
{ |
19
|
|
|
$breakerPos = mb_strpos($text, self::WYSIWYG_BREAK_HTML, null, 'UTF-8'); |
20
|
|
|
// offset is founded, try to split preview from full text |
21
|
|
|
if ($breakerPos !== false) { |
22
|
|
|
$text = Str::sub($text, 0, $breakerPos); |
23
|
|
|
} else { // page breaker is not founded, lets get a fun ;D |
24
|
|
|
// find first paragraph ending |
25
|
|
|
$breakerPos = mb_strpos($text, '</p>', null, 'UTF-8'); |
26
|
|
|
// no paragraph's ? lets try to get <br[\/|*]> |
27
|
|
|
if ($breakerPos === false) { |
28
|
|
|
$breakerPos = mb_strpos($text, '<br', null, 'UTF-8'); |
29
|
|
|
} else { |
30
|
|
|
// add length('</p>') |
|
|
|
|
31
|
|
|
$breakerPos+= 4; |
32
|
|
|
} |
33
|
|
|
// cut text from position caret before </p> (+4 symbols to save item as valid) |
34
|
|
|
if ($breakerPos !== false) { |
|
|
|
|
35
|
|
|
$text = Str::sub($text, 0, $breakerPos); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// if breaker position is still undefined - lets make 'text cut' for defined length and remove all html tags |
40
|
|
|
if ($breakerPos === false) { |
|
|
|
|
41
|
|
|
$text = strip_tags($text); |
42
|
|
|
$text = self::cut($text, 0, $length); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $text; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Cut text starting from $start pointer to $end pointer in UTF-8 mod |
50
|
|
|
* @param string $text |
51
|
|
|
* @param int $start |
52
|
|
|
* @param int $end |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public static function cut(string $text, int $start = 0, int $end = 0) |
56
|
|
|
{ |
57
|
|
|
return Str::sub($text, $start, $end); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.