dce7f1aeece87471576281a05584723f093cec72
[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 if (trim($text) == "") {
63 return;
64 }
65 $text = str_replace(" ", "", $text);
66 $text = str_replace(",", ";", $text);
67 $array = explode(";", $text);
68 return $array;
69 }
70
71 /** Returns a line of comma separated email addresses from an array **/
72 function getLineOfAddrs($array) {
73 $to_line = "";
74 for ($i = 0; $i < count($array); $i++) {
75 if ($to_line)
76 $to_line = "$to_line, $array[$i]";
77 else
78 $to_line = "$array[$i]";
79 }
80 return $to_line;
81 }
82
83 /* SquirrelMail version number -- DO NOT CHANGE */
84 $version = "0.2";
85 ?>