66cd5de8d562f45f1d26d527163af7cb19056b61
[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 $words = explode(" ", trim($passed));
32 $i = 0;
33 $line_len = strlen($words[$i])+1;
34 $line = "";
35 while ($i < count($words)) {
36 while ($line_len < $wrap) {
37 $line = "$line$words[$i]&nbsp;";
38 $i++;
39 $line_len = $line_len + strlen($words[$i])+1;
40 }
41 $line_len = strlen($words[$i])+1;
42 if ($line_len < $wrap) {
43 if ($i < count($words)) // don't <BR> the last line
44 $line = "$line<BR>";
45 } else {
46 $endline = $words[$i];
47 while ($line_len >= $wrap) {
48 $bigline = substr($endline, 0, $wrap);
49 $endline = substr($endline, $wrap, strlen($endline));
50 $line_len = strlen($endline);
51 $line = "$line$bigline<BR>";
52 }
53 $line = "$line$endline<BR>";
54 $i++;
55 }
56 }
57 return $line;
58 }
59
60 /** Returns an array of email addresses **/
61 function parseAddrs($text) {
62 $text = str_replace(" ", "", $text);
63 $text = str_replace(",", ";", $text);
64 $array = explode(";", $text);
65 return $array;
66 }
67
68 /** Returns a line of comma separated email addresses from an array **/
69 function getLineOfAddrs($array) {
70 $to_line = "";
71 for ($i = 0; $i < count($array); $i++) {
72 if ($to_line)
73 $to_line = "$to_line, $array[$i]";
74 else
75 $to_line = "$array[$i]";
76 }
77 return $to_line;
78 }
79
80 /* SquirrelMail version number -- DO NOT CHANGE */
81 $version = "0.2";
82 ?>