# String Object

**URL:** <https://forum.kirupa.com/t/string-object/177014>\
**Category:** open source\
**Created:** [January 29, 2006, 8:21am UTC](https://forum.kirupa.com/t/string-object/177014 "2006-01-29T08:21:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [January 29, 2006, 8:21am UTC](https://forum.kirupa.com/t/string-object/177014/1 "2006-01-29T08:21:35Z")

</div>

I’m not sure exactly what the point of this is, but I’ve basically reconstructed a String object in PHP.

It requires PHP5, as it uses Interfaces:

```php
<?php

/ *************************************
/* PRIMITIVE CLASSES
 * -----------------
 * 
 * Basically, this extends the default
 * functionality of primitives and
 * their related functions to be more
 * OOP. I'm not sure exactly what use
 * it has yet...
 ************************************ /

class String implements Iterator {
	/* Future Features:
	 * Ha! Caught up for now...
	 */
	
	public $string;
	private $position;

	public function __construct ($string) {
		$this->string = (string) $string;
	}

	public function getStr () { return $this->string; }
	public function __toString () { return $this->string; }

	/ **STRING FUNCTIONS** /
	
	// The destructive ones
	public function prepend ($str) { $this->string = $str.$this->string; }
	public function append ($str) { $this->string .= $str; }
	
	// The non-destructive ones
	public function strmsk ($char_list) {
		// Masks string to only contain letters in $char_list
		
		$haystack = str_split($this->string);
		$newstr = array(); // Array containing correct string
		foreach($haystack as $letter) {
			if(strpos($char_list,$letter)!==false) $newstr[] = $letter;
		}
		
		return implode('',$newstr);
	}

	// Iterator functions
	public function rewind () { $this->position=0; }
	public function current () { return $this->string{$this->position}; }
	public function key () { return $this->position; }
	public function next () { return $this->string{$this->position++}; }
		// Post-transform to include all letters in loop
	public function valid () { return ($this->position<strlen($this->string)); }
}

?>

```

Any suggestions for making this useful? Well, currently I find it really useful, as you can iterate through a string… I’ve always wanted this. But anything else?
