6 * This class is mainly used to implement the 'text-match' filter, used by both
7 * the CalDAV calendar-query REPORT, and CardDAV addressbook-query REPORT.
8 * Because they both need it, it was decided to put it in Sabre_DAV instead.
12 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
13 * @author Evert Pot (http://www.rooftopsolutions.nl/)
14 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
16 class Sabre_DAV_StringUtil {
19 * Checks if a needle occurs in a haystack ;)
21 * @param string $haystack
22 * @param string $needle
23 * @param string $collation
24 * @param string $matchType
27 static public function textMatch($haystack, $needle, $collation, $matchType = 'contains') {
31 case 'i;ascii-casemap' :
32 // default strtolower takes locale into consideration
33 // we don't want this.
34 $haystack = str_replace(range('a','z'), range('A','Z'), $haystack);
35 $needle = str_replace(range('a','z'), range('A','Z'), $needle);
42 case 'i;unicode-casemap' :
43 $haystack = mb_strtoupper($haystack, 'UTF-8');
44 $needle = mb_strtoupper($needle, 'UTF-8');
48 throw new Sabre_DAV_Exception_BadRequest('Collation type: ' . $collation . ' is not supported');
55 return strpos($haystack, $needle)!==false;
57 return $haystack === $needle;
59 return strpos($haystack, $needle)===0;
61 return strrpos($haystack, $needle)===strlen($haystack)-strlen($needle);
63 throw new Sabre_DAV_Exception_BadRequest('Match-type: ' . $matchType . ' is not supported');
70 * This method takes an input string, checks if it's not valid UTF-8 and
71 * attempts to convert it to UTF-8 if it's not.
73 * Note that currently this can only convert ISO-8559-1 to UTF-8 (latin-1),
74 * anything else will likely fail.
76 * @param string $input
79 static public function ensureUTF8($input) {
81 $encoding = mb_detect_encoding($input , array('UTF-8','ISO-8859-1'), true);
83 if ($encoding === 'ISO-8859-1') {
84 return utf8_encode($input);