Added limited support for Cyrillic (only KOI8-R yet) and made it possible to
[squirrelmail.git] / functions / strings.php
1 <?
2
3 $strings_php = true;
4
5 //*************************************************************************
6 // Count the number of occurances of $needle are in $haystack.
7 //*************************************************************************
8 function countCharInString($haystack, $needle) {
9 $len = strlen($haystack);
10 for ($i = 0; $i < $len; $i++) {
11 if ($haystack[$i] == $needle)
12 $count++;
13 }
14 return $count;
15 }
16
17 //*************************************************************************
18 // Read from the back of $haystack until $needle is found, or the begining
19 // of the $haystack is reached.
20 //*************************************************************************
21 function readShortMailboxName($haystack, $needle) {
22 if (substr($haystack, -1) == $needle)
23 $haystack = substr($haystack, 0, strlen($haystack) - 1);
24
25 if (strrpos($haystack, $needle)) {
26 $pos = strrpos($haystack, $needle) + 1;
27 $data = substr($haystack, $pos, strlen($haystack));
28 } else {
29 $data = $haystack;
30 }
31 return $data;
32 }
33
34 // Wraps text at $wrap characters
35 function wordWrap($passed, $wrap) {
36 $passed = str_replace("&gt;", ">", $passed);
37 $passed = str_replace("&lt;", "<", $passed);
38
39 $words = explode(" ", trim($passed));
40 $i = 0;
41 $line_len = strlen($words[$i])+1;
42 $line = "";
43 while ($i < count($words)) {
44 while ($line_len < $wrap) {
45 $line = "$line$words[$i] ";
46 $i++;
47 $line_len = $line_len + strlen($words[$i])+1;
48 }
49 $line_len = strlen($words[$i])+1;
50 if ($line_len < $wrap) {
51 if ($i < count($words)) // don't <BR> the last line
52 $line = "$line\n";
53 } else {
54 $endline = $words[$i];
55 while ($line_len >= $wrap) {
56 $bigline = substr($endline, 0, $wrap);
57 $endline = substr($endline, $wrap, strlen($endline));
58 $line_len = strlen($endline);
59 $line = "$line$bigline<BR>";
60 }
61 $line = "$line$endline<BR>";
62 $i++;
63 }
64 }
65
66 $line = str_replace(">", "&gt;", $line);
67 $line = str_replace("<", "&lt;", $line);
68 return $line;
69 }
70
71 /** Returns an array of email addresses **/
72 function parseAddrs($text) {
73 if (trim($text) == "") {
74 return;
75 }
76 $text = str_replace(" ", "", $text);
77 $text = str_replace(",", ";", $text);
78 $array = explode(";", $text);
79 return $array;
80 }
81
82 /** Returns a line of comma separated email addresses from an array **/
83 function getLineOfAddrs($array) {
84 $to_line = "";
85 for ($i = 0; $i < count($array); $i++) {
86 if ($to_line)
87 $to_line = "$to_line, $array[$i]";
88 else
89 $to_line = "$array[$i]";
90 }
91 return $to_line;
92 }
93
94 function translateText($body, $wrap_at, $charset) {
95 /** Add any parsing you want to in here */
96 $body = trim($body);
97 $body_ary = explode("\n", $body);
98
99 for ($i = 0; $i < count($body_ary); $i++) {
100 $line = $body_ary[$i];
101 $line = "^^$line";
102
103 //$line = str_replace(">", "&gt;", $line);
104 //$line = str_replace("<", "&lt;", $line);
105 //$line = htmlspecialchars($line);
106
107 if (strlen($line) >= $wrap_at) // -2 because of the ^^ at the beginning
108 $line = wordWrap($line, $wrap_at);
109
110 $line = charset_decode($charset, $line);
111
112 $line = str_replace(" ", "&nbsp;", $line);
113 $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
114 $line = nl2br($line);
115
116 if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
117 $line = substr($line, 2, strlen($line));
118 $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
119 } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
120 $line = substr($line, 2, strlen($line));
121 $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
122 } else {
123 $line = substr($line, 2, strlen($line));
124 $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
125 }
126
127 $new_body[$i] = "$line";
128 }
129 $bdy = implode("\n", $new_body);
130 return $bdy;
131 }
132
133 /* SquirrelMail version number -- DO NOT CHANGE */
134 $version = "0.4pre1";
135
136
137 function find_mailbox_name ($mailbox) {
138 $mailbox = trim($mailbox);
139 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
140 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
141 $pos = strrpos ($mailbox, "\"")+1;
142 $box = substr($mailbox, $pos);
143 } else {
144 $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
145 }
146 return $box;
147 }
148
149 function replace_spaces ($string) {
150 return str_replace(" ", "&nbsp;", $string);
151 }
152
153 function replace_escaped_spaces ($string) {
154 return str_replace("&nbsp;", " ", $string);
155 }
156
157 function count_chars($string) {
158 for ($i = 0; $i < strlen($string); $i++) {
159 $ch = substr($string, $i, 1);
160 $size++;
161 if ($ch == "\\") {
162 $i++;
163 $ch = substr($string, $i, 1);
164 if ($ch == "n")
165 $i--;
166 if ($ch == "r")
167 $i--;
168 }
169 }
170 return $size;
171 }
172 ?>