Created
January 6, 2020 14:17
-
-
Save kobus1998/482f22a7c8203d9e35233725f4392b04 to your computer and use it in GitHub Desktop.
object scalar string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Str | |
{ | |
private $str; | |
public function __construct(string $str) | |
{ | |
$this->str = $str; | |
} | |
public function replace($a, $b) | |
{ | |
$this->str = str_replace($a, $b, $this->str); | |
return $this; | |
} | |
public function ireplace($a, $b) | |
{ | |
$this->str = str_ireplace($a, $b, $this->str); | |
return $this; | |
} | |
public function toUpper() | |
{ | |
$this->str = strtoupper($this->str); | |
return $this; | |
} | |
public function toLower() | |
{ | |
$this->str = strtolower($this->str); | |
return $this; | |
} | |
public function append($str) | |
{ | |
$this->str .= $str; | |
return $this; | |
} | |
public function prepend($str) | |
{ | |
$this->str = $str . $this->str; | |
return $this; | |
} | |
public function trim() | |
{ | |
$this->str = trim($this->str); | |
return $this; | |
} | |
public function pregReplace($preg, $str) | |
{ | |
$this->str = preg_replace($preg, $str, $this->str); | |
} | |
public function get() | |
{ | |
return $this->str; | |
} | |
public function __toString() | |
{ | |
return $this->str; | |
} | |
} | |
$str = (string) (new Str('Hello World')) | |
->toUpper() | |
->ireplace('world', '') | |
->trim() | |
->append('!') | |
; | |
echo $str; // HELLO! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment