| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* LICENSE |
| 6 |
|
* |
| 7 |
|
* This source file is subject to the new BSD license that is bundled |
| 8 |
|
* with this package in the file LICENSE.txt. |
| 9 |
|
* It is also available through the world-wide-web at this URL: |
| 10 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 11 |
|
* If you did not receive a copy of the license and are unable to |
| 12 |
|
* obtain it through the world-wide-web, please send an email |
| 13 |
|
* to xyster@devweblog.org so we can send you a copy immediately. |
| 14 |
|
* |
| 15 |
|
* @category Xyster |
| 16 |
|
* @package Xyster |
| 17 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 18 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 19 |
|
* @version $Id: String.php 105 2007-10-02 23:32:38Z doublecompile $ |
| 20 |
|
*/ |
| 21 |
|
/** |
| 22 |
|
* Helper class for string manipulation |
| 23 |
|
* |
| 24 |
|
* @category Xyster |
| 25 |
|
* @package Xyster |
| 26 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 27 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 28 |
|
*/ |
| 29 |
|
class Xyster_String |
| 30 |
|
{ |
| 31 |
|
/** |
| 32 |
|
* Used for matching parenthesis groups not inside quotes |
| 33 |
|
* |
| 34 |
|
* It works, but this still matches parentheses inside quotes that aren't in |
| 35 |
|
* parentheses. |
| 36 |
|
*/ |
| 37 |
|
const PARENTH_QUOTE_REGEX = '/(?<![\w\d])(\(((?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^()])|(?1))+\))/m'; |
| 38 |
|
|
| 39 |
|
/** |
| 40 |
|
* An array of words that shouldn't be capitalized in title case |
| 41 |
|
* |
| 42 |
|
* @var array |
| 43 |
|
*/ |
| 44 |
|
static private $_smallWords = array( 'of','a','the','and','an','or','nor', |
| 45 |
|
'but','is','if','then','else','when', 'at','from','by','on','off','for', |
| 46 |
|
'in','out','over','to','into','with' ); |
| 47 |
|
|
| 48 |
|
/** |
| 49 |
|
* Converts an associative array into a string representation |
| 50 |
|
* |
| 51 |
|
* For instance, an example output of this method might look like: |
| 52 |
|
* |
| 53 |
|
* <code>1=January,2=February,3=March</code> |
| 54 |
|
* |
| 55 |
|
* @param array $array The array to stringify |
| 56 |
|
* @return string The stringified array |
| 57 |
|
*/ |
| 58 |
|
static function arrayToString( array $array ) |
| 59 |
|
{ |
| 60 |
68 |
$string = array(); |
| 61 |
68 |
foreach( $array as $key => $value ) { |
| 62 |
68 |
$string[] = $key . '=' . $value; |
| 63 |
68 |
} |
| 64 |
68 |
return implode(',', $string); |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
/** |
| 68 |
|
* Match nested parentheses groups not inside of double-quotes |
| 69 |
|
* |
| 70 |
|
* This method will find all balanced parentheses not inside of double |
| 71 |
|
* quotes. It also respects escaped double quotes (i.e. with a preceding |
| 72 |
|
* backslash). |
| 73 |
|
* |
| 74 |
|
* For example, if you ran this method on a string containing JavaScript |
| 75 |
|
* code: |
| 76 |
|
* <code> |
| 77 |
|
* $string = <<<HEREDOC |
| 78 |
|
* var n = navigator; |
| 79 |
|
* return ( n.appName == "Netscape" ) || n.appVersion != "5.0 (Windows; en-US)"; |
| 80 |
|
* HEREDOC; |
| 81 |
|
* </code> |
| 82 |
|
* |
| 83 |
|
* You would receive back an array containing: |
| 84 |
|
* <pre> |
| 85 |
|
* array ( |
| 86 |
|
* [0]=>( n.appName == "Netscape" ) |
| 87 |
|
* ); |
| 88 |
|
* </pre> |
| 89 |
|
* |
| 90 |
|
* The second matched parentheses are inside of quotation marks and thus |
| 91 |
|
* should be treated as part of that string literal. |
| 92 |
|
* |
| 93 |
|
* @param string $string |
| 94 |
|
* @return array |
| 95 |
|
* @todo make this work with top-level strings with parenths in them |
| 96 |
|
*/ |
| 97 |
|
static public function matchGroups( $string ) |
| 98 |
|
{ |
| 99 |
51 |
if ( strpos($string, '(') === false ) { |
| 100 |
50 |
return array(); |
| 101 |
0 |
} |
| 102 |
|
|
| 103 |
34 |
$matches = array(); |
| 104 |
34 |
preg_match_all(self::PARENTH_QUOTE_REGEX, $string, $matches, PREG_SET_ORDER); |
| 105 |
|
|
| 106 |
34 |
$groups = array(); |
| 107 |
34 |
foreach( $matches as $group ) { |
| 108 |
32 |
$groups[] = $group[0]; |
| 109 |
32 |
} |
| 110 |
34 |
return $groups; |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
/** |
| 114 |
|
* Like explode(), but won't split inside of parentheses or double-quotes |
| 115 |
|
* |
| 116 |
|
* This method will split a string into an array using a string as a |
| 117 |
|
* seperator. If the seperator is contained within a pair of double-quotes |
| 118 |
|
* or between matching parentheses, it will be ignored. If the seperator is |
| 119 |
|
* not found, the method will return an array with one element containing |
| 120 |
|
* the entire string. |
| 121 |
|
* |
| 122 |
|
* Example: |
| 123 |
|
* <code> |
| 124 |
|
* $haystack = 'A (great (test)) of "this method"'; |
| 125 |
|
* print_r(Xyster_String::smartSplit(' ', $haystack)); |
| 126 |
|
* </code> |
| 127 |
|
* |
| 128 |
|
* Would print: |
| 129 |
|
* <pre> |
| 130 |
|
* array ( |
| 131 |
|
* [0]=>A |
| 132 |
|
* [1]=>(great (test)) |
| 133 |
|
* [2]=>of |
| 134 |
|
* [3]=>"this method" |
| 135 |
|
* ) |
| 136 |
|
* </pre> |
| 137 |
|
* |
| 138 |
|
* @param string $needle String used as seperator |
| 139 |
|
* @param string $haystack String to split |
| 140 |
|
* @param bool $caseInsensitive Whether to ignore case |
| 141 |
|
* @return array Split parts of $haystack |
| 142 |
|
*/ |
| 143 |
|
static public function smartSplit( $needle, $haystack, $caseInsensitive=true ) |
| 144 |
|
{ |
| 145 |
77 |
$split = array(); |
| 146 |
77 |
$buff = ""; |
| 147 |
77 |
$inPar = 0; |
| 148 |
77 |
$inStr = false; |
| 149 |
77 |
for( $i=0; $i<strlen($haystack); $i++ ) { |
| 150 |
77 |
$currNeed = substr( $haystack, $i, strlen($needle) ); |
| 151 |
77 |
$curr = $currNeed[0]; |
| 152 |
77 |
$last = ( $i ) ? $haystack[$i-1] : ""; |
| 153 |
77 |
if ( $curr == '"' && ( ( $inStr && $last != "\\") || !$inStr ) ) { |
| 154 |
22 |
$inStr = !$inStr; |
| 155 |
22 |
} |
| 156 |
77 |
if ( !$inStr ) { |
| 157 |
77 |
if ( $curr == "(" ) { |
| 158 |
24 |
$inPar++; |
| 159 |
77 |
} else if ( $curr == ")" ) { |
| 160 |
24 |
$inPar--; |
| 161 |
24 |
} |
| 162 |
77 |
} |
| 163 |
77 |
if ( !$inPar && !$inStr && |
| 164 |
77 |
( $currNeed == $needle || |
| 165 |
77 |
( $caseInsensitive && strcasecmp($currNeed,$needle) == 0 ) ) ) { |
| 166 |
58 |
$split[] = $buff; |
| 167 |
58 |
$buff = ""; |
| 168 |
58 |
$i = $i+(strlen($needle)-1); |
| 169 |
58 |
} else { |
| 170 |
77 |
$buff .= $curr; |
| 171 |
|
} |
| 172 |
77 |
} |
| 173 |
77 |
$split[] = $buff; |
| 174 |
77 |
return $split; |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
/** |
| 178 |
|
* Converts a string to title case |
| 179 |
|
* |
| 180 |
|
* @return string The input in title case |
| 181 |
|
*/ |
| 182 |
|
static function titleCase( $title ) |
| 183 |
|
{ |
| 184 |
|
// Split the string into separate words |
| 185 |
1 |
$words = explode(' ', strtolower($title)); |
| 186 |
|
|
| 187 |
1 |
foreach ( $words as $key => $word ) { |
| 188 |
|
// If this is the first, or it's not a small word, capitalize it |
| 189 |
1 |
if ( $key == 0 or !in_array($word, self::$_smallWords) ) { |
| 190 |
1 |
$words[$key] = ucwords($word); |
| 191 |
1 |
} |
| 192 |
1 |
} |
| 193 |
|
|
| 194 |
1 |
return implode(' ', $words); |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
/** |
| 198 |
|
* Converts a string in Underscore case to Camel case |
| 199 |
|
* |
| 200 |
|
* <code> |
| 201 |
|
* $in = "this_is_underscore_case"; |
| 202 |
|
* $out = Xyster_String::toCamel($in); |
| 203 |
|
* echo $out; // prints thisIsCamelCase |
| 204 |
|
* </code> |
| 205 |
|
* |
| 206 |
|
* @param string $name A string in Underscore case (this_is_underscore_case) |
| 207 |
|
* @return string The string in Camel case (thisIsCamelCase) |
| 208 |
|
* @see toUnderscores() |
| 209 |
|
*/ |
| 210 |
|
static public function toCamel( $name ) |
| 211 |
|
{ |
| 212 |
215 |
return preg_replace("/_([a-z])/e", "strtoupper('\\1')", $name); |
| 213 |
|
} |
| 214 |
|
|
| 215 |
|
/** |
| 216 |
|
* Converts a string in Camel case to Underscore case |
| 217 |
|
* |
| 218 |
|
* <code> |
| 219 |
|
* $in = "thisIsCamelCase"; |
| 220 |
|
* $out = Xyster_String::toUnderscores($in); |
| 221 |
|
* echo $out; // prints this_is_underscore_case |
| 222 |
|
* </code> |
| 223 |
|
* |
| 224 |
|
* @param string $name A string in Camel case (thisIsCamelCase) |
| 225 |
|
* @return string The string in Underscore case (this_is_underscore_case) |
| 226 |
|
* @see toCamel() |
| 227 |
|
*/ |
| 228 |
|
static public function toUnderscores( $name ) |
| 229 |
|
{ |
| 230 |
197 |
return strtolower(preg_replace('/([a-z])([A-Z])/', "$1_$2", $name)); |
| 231 |
|
} |
| 232 |
|
} |