11e30f924563c2007b22128f77caeb6fb72f89e2
[squirrelmail.git] / functions / strings.php
1 <?
2
3 //*************************************************************************
4 // Count the number of occurances of $needle are in $haystack.
5 //*************************************************************************
6 function countCharInString($haystack, $needle) {
7 $len = strlen($haystack);
8 for ($i = 0; $i < $len; $i++) {
9 if ($haystack[$i] == $needle)
10 $count++;
11 }
12 return $count;
13 }
14
15 //*************************************************************************
16 // Read from the back of $haystack until $needle is found, or the begining
17 // of the $haystack is reached.
18 //*************************************************************************
19 function readShortMailboxName($haystack, $needle) {
20 if (substr($haystack, -1) == $needle)
21 $haystack = substr($haystack, 0, strlen($haystack) - 1);
22
23 if (strpos($haystack, $needle)) {
24 $pos = strrpos($haystack, $needle) + 1;
25 $data = substr($haystack, $pos, strlen($haystack));
26 } else {
27 $data = $haystack;
28 }
29 return $data;
30 }
31
32 // Wraps text at $wrap characters
33 function wordWrap($passed, $wrap) {
34 $passed = str_replace("&gt;", ">", $passed);
35 $passed = str_replace("&lt;", "<", $passed);
36
37 $words = explode(" ", trim($passed));
38 $i = 0;
39 $line_len = strlen($words[$i])+1;
40 $line = "";
41 while ($i < count($words)) {
42 while ($line_len < $wrap) {
43 $line = "$line$words[$i] ";
44 $i++;
45 $line_len = $line_len + strlen($words[$i])+1;
46 }
47 $line_len = strlen($words[$i])+1;
48 if ($line_len < $wrap) {
49 if ($i < count($words)) // don't <BR> the last line
50 $line = "$line\n";
51 } else {
52 $endline = $words[$i];
53 while ($line_len >= $wrap) {
54 $bigline = substr($endline, 0, $wrap);
55 $endline = substr($endline, $wrap, strlen($endline));
56 $line_len = strlen($endline);
57 $line = "$line$bigline<BR>";
58 }
59 $line = "$line$endline<BR>";
60 $i++;
61 }
62 }
63
64 $line = str_replace(">", "&gt;", $line);
65 $line = str_replace("<", "&lt;", $line);
66 return $line;
67 }
68
69 /** Returns an array of email addresses **/
70 function parseAddrs($text) {
71 if (trim($text) == "") {
72 return;
73 }
74 $text = str_replace(" ", "", $text);
75 $text = str_replace(",", ";", $text);
76 $array = explode(";", $text);
77 return $array;
78 }
79
80 /** Returns a line of comma separated email addresses from an array **/
81 function getLineOfAddrs($array) {
82 $to_line = "";
83 for ($i = 0; $i < count($array); $i++) {
84 if ($to_line)
85 $to_line = "$to_line, $array[$i]";
86 else
87 $to_line = "$array[$i]";
88 }
89 return $to_line;
90 }
91
92 function translateText($body, $wrap_at) {
93 /** Add any parsing you want to in here */
94 $body = trim($body);
95 $body_ary = explode("\n", $body);
96
97 for ($i = 0; $i < count($body_ary); $i++) {
98 $line = $body_ary[$i];
99 $line = "^^$line";
100
101 $line = str_replace(">", "&gt;", $line);
102 $line = str_replace("<", "&lt;", $line);
103
104 if (strlen($line) >= $wrap_at) // -2 because of the ^^ at the beginning
105 $line = wordWrap($line, $wrap_at);
106
107 $line = str_replace(" ", "&nbsp;", $line);
108 $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
109 $line = nl2br($line);
110
111 if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
112 $line = substr($line, 2, strlen($line));
113 $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
114 } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
115 $line = substr($line, 2, strlen($line));
116 $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
117 } else {
118 $line = substr($line, 2, strlen($line));
119 $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
120 }
121
122 $new_body[$i] = "$line";
123 }
124 $bdy = implode("\n", $new_body);
125 return $bdy;
126 }
127
128 /* SquirrelMail version number -- DO NOT CHANGE */
129 $version = "0.3pre1";
130 ?>