JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
clean up my urls
[wfpl.git] / string_array.php
1 <?php
2
3 #  Copyright (C) 2007 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file contains code to convert an array of strings into a string, and back again.
20
21 function string_to_array($data) {
22         $pos = 0;
23         $max = strlen($data);
24         $out = array();
25         while($pos < $max) {
26                 $sep = stripos($data, ' ', $pos);
27                 if ($sep == -1 || $sep == $pos) {
28                         return $out;
29                 }
30                 $size = (int) substr($data, $pos, $sep - $pos);
31                 $pos = $sep + 1;
32                 $out[] = substr($data, $pos, $size);
33                 $pos += $size;
34         }
35         return $out;
36 }
37
38 function array_to_string($array) {
39         $ret = '';
40         foreach($array as &$element) {
41                 if (is_string($element)) {
42                         $ret .= strlen($element) . ' ';
43                         $ret .= $element;
44                 }
45         } unset ($element);
46         return $ret;
47 }
48
49 ?>