├── README ├── examples.php └── string.php /README: -------------------------------------------------------------------------------- 1 | Provides an equivalent of Javascript's string class. It also uses 2 | mbstring functions by default if you have mbstring enabled. 3 | 4 | See examples.php for extensive examples. 5 | 6 | Require PHP 5.2+, but PHP 5.3 is preferred. -------------------------------------------------------------------------------- /examples.php: -------------------------------------------------------------------------------- 1 | split()->each(function ($value, $key) { 23 | echo "{$key}: {$value}\n"; 24 | }); 25 | 26 | // it works in foreach too. 27 | // this is gives the same output as above 28 | foreach($a->split() as $key => $value) { 29 | echo "{$key}: {$value}\n"; 30 | } 31 | 32 | // regex too! 33 | // output: 34 | /* 35 | Number found: 9 36 | Number found: 1000 37 | */ 38 | $b = new String('This 9 word sentence is less than 1000 letters long.'); 39 | $b->match("/([0-9]+)/")->each(function ($value) { 40 | echo "Number found: {$value}\n"; 41 | }); 42 | 43 | // replaces all numbers with NUMBER. Third arg must be true because last arg is a regular expression 44 | // output: This NUMBER word sentence is less than NUMBER letters long. 45 | echo $b->replace("/([0-9]+)/", 'NUMBER', true)."\n"; 46 | 47 | // not using regular expressions, and lowercase 48 | // output: this9wordsentenceislessthan1000letterslong. 49 | echo $b->replace(' ', '')->toLower()."\n"; 50 | 51 | // something a bit more complex 52 | // strips the trailing 0's and 9's and splits it into an array, adds an element, reverse sorts it and turns it into an array 53 | // output: 54 | /* 55 | Array 56 | ( 57 | [8] => hi there! 58 | [7] => 8 59 | [6] => 7 60 | [5] => 6 61 | [4] => 5 62 | [3] => 4 63 | [2] => 3 64 | [1] => 2 65 | [0] => 1 66 | ) 67 | */ 68 | print_r($a->rtrim('09')->split()->push('hi there!')->rsort()->toArray()); 69 | 70 | 71 | // turning a standard array into a special array 72 | $c = new Arr(array('hi', 'there', 'how', 'are', 'you?')); 73 | 74 | // output: hi there how are you? 75 | echo $c->join(' ')."\n"; 76 | 77 | // you can still use the String class like an array: 78 | $d = new String('012345'); 79 | 80 | // output: string(1) "3" 81 | var_dump($d[3]); 82 | -------------------------------------------------------------------------------- /string.php: -------------------------------------------------------------------------------- 1 | value = (string)$string; 151 | } 152 | 153 | public function __toString () { 154 | return $this->value; 155 | } 156 | 157 | /* end magic methods */ 158 | 159 | /* ArrayAccess Methods */ 160 | 161 | /** offsetExists ( mixed $index ) 162 | * 163 | * Similar to array_key_exists 164 | */ 165 | public function offsetExists ($index) { 166 | return !empty($this->value[$index]); 167 | } 168 | 169 | /* offsetGet ( mixed $index ) 170 | * 171 | * Retrieves an array value 172 | */ 173 | public function offsetGet ($index) { 174 | return StaticString::substr($this->value, $index, 1)->toString(); 175 | } 176 | 177 | /* offsetSet ( mixed $index, mixed $val ) 178 | * 179 | * Sets an array value 180 | */ 181 | public function offsetSet ($index, $val) { 182 | $this->value = StaticString::substring($this->value, 0, $index) . $val . StaticString::substring($this->value, $index+1, StaticString::strlen($this->value)); 183 | } 184 | 185 | /* offsetUnset ( mixed $index ) 186 | * 187 | * Removes an array value 188 | */ 189 | public function offsetUnset ($index) { 190 | $this->value = StaticString::substr($this->value, 0, $index) . StaticString::substr($this->value, $index+1); 191 | } 192 | 193 | public static function create ($obj) { 194 | if($obj instanceof String) return new String($obj); 195 | return new String($obj); 196 | } 197 | 198 | /* public methods */ 199 | public function substr ($start, $length) { 200 | return StaticString::substr($this->value, $start, $length); 201 | } 202 | 203 | public function substring ($start, $end) { 204 | return StaticString::substring($this->value, $start, $end); 205 | } 206 | 207 | public function charAt ($point) { 208 | return StaticString::substr($this->value, $point, 1); 209 | } 210 | 211 | public function charCodeAt ($point) { 212 | return ord(StaticString::substr($this->value, $point, 1)); 213 | } 214 | 215 | public function indexOf ($needle, $offset) { 216 | return StaticString::indexOf($this->value, $needle, $offset); 217 | } 218 | 219 | public function lastIndexOf ($needle) { 220 | return StaticString::lastIndexOf($this->value, $needle); 221 | } 222 | 223 | public function match ($regex) { 224 | return StaticString::match($this->value, $regex); 225 | } 226 | 227 | public function replace ($search, $replace, $regex = false) { 228 | return StaticString::replace($this->value, $search, $replace, $regex); 229 | } 230 | 231 | public function first () { 232 | return StaticString::substr($this->value, 0, 1); 233 | } 234 | 235 | public function last () { 236 | return StaticString::substr($this->value, -1, 1); 237 | } 238 | 239 | public function search ($search, $offset = null) { 240 | return $this->indexOf($search, $offset); 241 | } 242 | 243 | public function slice ($start, $end = null) { 244 | return StaticString::slice($this->value, $start, $end); 245 | } 246 | 247 | public function toLowerCase () { 248 | return StaticString::toLowerCase($this->value); 249 | } 250 | 251 | public function toUpperCase () { 252 | return StaticString::toUpperCase($this->value); 253 | } 254 | 255 | public function toUpper () { 256 | return $this->toUpperCase(); 257 | } 258 | 259 | public function toLower () { 260 | return $this->toLowerCase(); 261 | } 262 | 263 | public function split ($at = '') { 264 | return StaticString::split($this->value, $at); 265 | } 266 | 267 | public function trim ($charlist = null) { 268 | return new String(trim($this->value, $charlist)); 269 | } 270 | 271 | public function ltrim ($charlist = null) { 272 | return new String(ltrim($this->value, $charlist)); 273 | } 274 | 275 | public function rtrim ($charlist = null) { 276 | return new String(rtrim($this->value, $charlist)); 277 | } 278 | 279 | public function toString () { 280 | return $this->__toString(); 281 | } 282 | } 283 | class Arr extends ArrayObject { 284 | private static $ret_obj = true; 285 | 286 | public function add () { 287 | $val = 0; 288 | foreach($this as $vals) { 289 | $val += $vals; 290 | } 291 | return $val; 292 | } 293 | 294 | public function get ($i) { 295 | $val = $this->offsetGet($i); 296 | if(is_array($val)) { 297 | return new self($val); 298 | } 299 | if(is_string($val) && self::$ret_obj) { 300 | return new String($val); 301 | } 302 | return $val; 303 | } 304 | 305 | public function each ($callback) { 306 | foreach($this as $key => $val) { 307 | call_user_func_array($callback, array( 308 | $val, $key, $this 309 | )); 310 | } 311 | return $this; 312 | } 313 | 314 | public function set ($i, $v) { 315 | $this->offsetSet($i, $v); 316 | return $this; 317 | } 318 | 319 | public function push ($value) { 320 | $this[] = $value; 321 | return $this; 322 | } 323 | 324 | public function join ($paste = '') { 325 | return implode($paste, $this->getArrayCopy()); 326 | } 327 | 328 | public function sort () { 329 | $this->asort(); 330 | return $this; 331 | } 332 | 333 | public function toArray () { 334 | return $this->getArrayCopy(); 335 | } 336 | 337 | public function natsort () { 338 | parent::natsort(); 339 | return $this; 340 | } 341 | 342 | public function rsort () { 343 | parent::uasort('Arr::sort_alg'); 344 | return $this; 345 | } 346 | 347 | public static function sort_alg ($a,$b) { 348 | if ($a == $b) { 349 | return 0; 350 | } 351 | return ($a < $b) ? 1 : -1; 352 | } 353 | } 354 | 355 | --------------------------------------------------------------------------------