c3cdc3a31091ae13a9c54365a585f380684dc760
[squirrelmail.git] / functions / strings.php
1 <?
2 //*************************************************************************
3 // Count the number of occurances of $needle are in $haystack.
4 //*************************************************************************
5 function countCharInString($haystack, $needle) {
6 $len = strlen($haystack);
7 for ($i = 0; $i < $len; $i++) {
8 if ($haystack[$i] == $needle)
9 $count++;
10 }
11 return $count;
12 }
13
14 //*************************************************************************
15 // Read from the back of $haystack until $needle is found, or the begining
16 // of the $haystack is reached.
17 //*************************************************************************
18 function readShortMailboxName($haystack, $needle) {
19 if (strpos($haystack, $needle)) {
20 $pos = strrpos($haystack, $needle) + 1;
21 $data = substr($haystack, $pos, strlen($haystack));
22 } else {
23 $data = $haystack;
24 }
25 return $data;
26 }
27
28 // Wraps text at $wrap characters
29 function wordWrap($passed, $wrap) {
30 $words = explode(" ", trim($passed));
31 $i = 0;
32 $line_len = strlen($words[$i])+1;
33 $line = "";
34 while ($i < count($words)) {
35 while ($line_len < $wrap) {
36 $line = "$line$words[$i]&nbsp;";
37 $i++;
38 $line_len = $line_len + strlen($words[$i])+1;
39 }
40 $line_len = strlen($words[$i])+1;
41 if ($line_len < $wrap) {
42 if ($i < count($words)) // don't <BR> the last line
43 $line = "$line<BR>";
44 } else {
45 $endline = $words[$i];
46 while ($line_len >= $wrap) {
47 $bigline = substr($endline, 0, $wrap);
48 $endline = substr($endline, $wrap, strlen($endline));
49 $line_len = strlen($endline);
50 $line = "$line$bigline<BR>";
51 }
52 $line = "$line$endline<BR>";
53 $i++;
54 }
55 }
56 return $line;
57 }
58
59 /** Returns an array of email addresses **/
60 function parseAddrs($text) {
61 $text = str_replace(" ", "", $text);
62 $text = str_replace(",", ";", $text);
63 $array = explode(";", $text);
64 return $array;
65 }
66
67 /** Returns a line of comma separated email addresses from an array **/
68 function getLineOfAddrs($array) {
69 $to_line = "";
70 for ($i = 0; $i < count($array); $i++) {
71 if ($to_line)
72 $to_line = "$to_line, $array[$i]";
73 else
74 $to_line = "$array[$i]";
75 }
76 return $to_line;
77 }
78 ?>