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