Subversion Repositories oidplus

Rev

Rev 1385 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1385 Rev 1389
Line 766... Line 766...
766
        }
766
        }
767
        return $cleartext;
767
        return $cleartext;
768
}
768
}
769
 
769
 
770
/**
770
/**
-
 
771
 * Finds a substring that is guaranteed not in $str
-
 
772
 * @param string $str
-
 
773
 * @return string
-
 
774
 */
-
 
775
function find_nonexisting_substr(string $str): string {
-
 
776
        $i = 0;
-
 
777
        do {
-
 
778
                $i++;
-
 
779
                $dummy = "[$i]";
-
 
780
        } while (strpos($str, $dummy) !== false);
-
 
781
        return $dummy;
-
 
782
}
-
 
783
 
-
 
784
/**
771
 * Works like explode(), but it respects if $separator is preceded by an backslash escape character
785
 * Works like explode(), but it respects if $separator is preceded by an backslash escape character
772
 * @param string $separator
786
 * @param string $separator
773
 * @param string $string
787
 * @param string $string
774
 * @param int $limit
788
 * @param int $limit
775
 * @return array
789
 * @return array
776
 */
790
 */
777
function explode_with_escaping(string $separator, string $string, int $limit=PHP_INT_MAX): array {
791
function explode_with_escaping(string $separator, string $string, int $limit=PHP_INT_MAX): array {
-
 
792
        $dummy1 = find_nonexisting_substr($string);
-
 
793
        $dummy2 = find_nonexisting_substr($string.$dummy1);
-
 
794
 
778
        $string = str_replace('\\\\', chr(2), $string);
795
        $string = str_replace('\\\\', $dummy2, $string);
779
        $string = str_replace('\\'.$separator, chr(1), $string);
796
        $string = str_replace('\\'.$separator, $dummy1, $string);
780
 
797
 
781
        $ary = explode($separator, $string, $limit);
798
        $ary = explode($separator, $string, $limit);
782
 
799
 
783
        foreach ($ary as &$a) {
800
        foreach ($ary as &$a) {
784
                $a = str_replace(chr(2), '\\\\', $a);
801
                $a = str_replace($dummy2, '\\\\', $a);
785
                $a = str_replace(chr(1), '\\'.$separator, $a);
802
                $a = str_replace($dummy1, '\\'.$separator, $a);
786
        }
803
        }
787
        unset($a);
804
        unset($a);
788
 
805
 
789
        return $ary;
806
        return $ary;
790
}
807
}