Skip to content

Instantly share code, notes, and snippets.

@Shoghy
Last active February 16, 2024 11:47
Show Gist options
  • Save Shoghy/95a29fc1fc26a91cfedebaff0ec56f39 to your computer and use it in GitHub Desktop.
Save Shoghy/95a29fc1fc26a91cfedebaff0ec56f39 to your computer and use it in GitHub Desktop.
A PHP class that saves a string and implements string methods
<?php
/**
* @property-read int $length
*/
class BetterStr{
protected string $value = "";
public function get_value(){
return $this->value;
}
public function __construct(string $value = ""){
$this->value = $value;
}
public function __toString(){
return $this->value;
}
public static function concat(string ...$params){
return static::fromArray($params);
}
public function replace(string $search, string $replace, ?int &$count = null, bool $case_sensitive = true){
$value = "";
if($case_sensitive){
$value = str_replace($search, $replace, $this->value, $count);
}else{
$value = str_ireplace($search, $replace, $this->value, $count);
}
return new BetterStr($value);
}
public function contains(string $needle){
return str_contains($this->value, $needle);
}
public function ends_with(string $needle){
return str_ends_with($this->value, $needle);
}
public function __get(string $name){
switch($name){
case "length":{
return strlen($this->value);
}
}
}
public function right_pad(int $length, string $pad_string = " "){
$value = str_pad($this->value, $length, $pad_string, STR_PAD_RIGHT);
return new BetterStr($value);
}
public function left_pad(int $length, string $pad_string = " "){
$value = str_pad($this->value, $length, $pad_string, STR_PAD_LEFT);
return new BetterStr($value);
}
public function pad(int $length, string $pad_string = " "){
$value = str_pad($this->value, $length, $pad_string, STR_PAD_BOTH);
return new BetterStr($value);
}
public function repeat(int $times){
$value = str_repeat($this->value, $times);
return new BetterStr($value);
}
public function shuffle(){
$value = str_shuffle($this->value);
return new BetterStr($value);
}
/** @return array<BetterStr> */
public function fragment(int $fragment_size = 1){
$values = str_split($this->value, $fragment_size);
return static::createArray($values);
}
/** @return array<BetterStr> */
public function split(string $separator, int $limit = PHP_INT_MAX){
$values = explode($separator, $this->value, $limit);
return static::createArray($values);
}
public function starts_with(string $needle){
return str_starts_with($this->value, $needle);
}
/** @return array<BetterStr> */
public function word_count(int $format = 0, ?string $characters = null){
$values = str_word_count($this->value, $format, $characters);
return static::createArray($values);
}
public function trim(string $characters = " \n\r\t\v\x00"){
return new BetterStr(trim($this->value, $characters));
}
public function substr(int $offset, ?int $length = null){
$value = substr($this->value, $offset, $length);
return new BetterStr($value);
}
public function rtrim(string $characters = " \n\r\t\v\x00"){
$value = rtrim($this->value, $characters);
return new BetterStr($value);
}
public function ltrim(string $characters = " \n\r\t\v\x00"){
$value = ltrim($this->value, $characters);
return new BetterStr($value);
}
public function count_chars(int $mode = 0){
return count_chars($this->value, $mode);
}
public function crypt(string $salt){
$value = crypt($this->value, $salt);
return new BetterStr($value);
}
public static function fromArray(array $arr, string $separator = ""){
$value = implode($separator, $arr);
return new BetterStr($value);
}
/**
* @param array<string> $arr
* @return array<BetterStr>
*/
public static function createArray(array $arr){
$bttrarr = [];
foreach($arr as $item){
$bttrarr[] = new BetterStr($item);
}
return $bttrarr;
}
public function toLowerCase(){
$value = strtolower($this->value);
return new BetterStr($value);
}
public function toUpperCase(){
$value = strtoupper($this->value);
return new BetterStr($value);
}
public function wordwrap(int $width = 75, string $break = "\n", bool $cut_long_words = false){
$value = wordwrap($this->value, $width, $break, $cut_long_words);
return new BetterStr($value);
}
public function __clone(){
$this->value = clone $this->value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment