pluf2

pluf2 Commit Details


Date:2009-06-26 05:13:02 (15 years 5 months ago)
Author:Loic d'Anterroches
Branch:master
Commit:dbba527c951ce0a1d91b58900ef5dc3e1eabd445
Parents: b497e5b1a81f7602ff15aba86134afd29c20c7ad
Message:Added a wordwrap method not breaking the HTML.

Changes:

File differences

src/Pluf/Text.php
2828
2929
3030
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
89
90
91
92
93
94
95
96
97
98
99
100
31101
32102
33103
class Pluf_Text
{
/**
* Wrap a string containing HTML code.
*
* The HTML is not broken, words are broken only if very long.
*
* Improved from a version available on php.net
*
* @see http://www.php.net/manual/en/function.wordwrap.php#89782
*
* @param string The string to wrap
* @param int The maximal length of a string (45)
* @param string Wrap string ("\n")
* @return string Wrapped string
*/
public static function wrapHtml($string, $length=45, $wrapString="\n")
{
$wrapped = '';
$word = '';
$html = false;
$line_len = 0;
$n = mb_strlen($string);
for ($i=0; $i<$n; $i++) {
$char = mb_substr($string, $i, 1);
/** HTML Begins */
if ($char === '<') {
if(!empty($word)) {
$wrapped .= $word;
$word = '';
}
$html = true;
$wrapped .= $char;
} elseif ($char === '>') {
/** HTML ends */
$html = false;
$wrapped .= $char;
} elseif ($html) {
/** If this is inside HTML -> append to the wrapped string */
$wrapped .= $char;
} elseif ($char === "\n") {
/** Whitespace characted / new line */
$wrapped .= $word.$char;
$word = '';
$line_len = 0;
} elseif ($char === ' ' || $char === "\t") {
$word .= $char;
if(strlen($word) + $line_len <= $length) {
$line_len += strlen($word);
$wrapped .= $word;
$word = '';
}
} else {
/** Check chars */
$word .= $char;
if (mb_strlen($word) >= $length) {
$wrapped .= $word.$wrapString;
$word = '';
$line_len = 0;
} elseif (mb_strlen($word) + $line_len > $length) {
$wrapped .= $wrapString;
$line_len = 0;
}
}
}
if ($word !== '') {
$wrapped .= $word;
}
return $wrapped;
}
/**
* Given a string, cleaned from the not interesting characters,
* returns an array with the words as index and the number of
* times it was in the text as the value.

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.05317s using 13 queries.