Skip to content

Instantly share code, notes, and snippets.

@ildarkhasanshin
Created February 19, 2019 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ildarkhasanshin/2965a90376a9caa413dc1d6e5e1dde29 to your computer and use it in GitHub Desktop.
Save ildarkhasanshin/2965a90376a9caa413dc1d6e5e1dde29 to your computer and use it in GitHub Desktop.
<?php
namespace System;
// простая функция для создания экземпляра класса
function strs($php_string) {
return new Strings($php_string);
}
class Strings
{
private $string;
public function __construct( $string ) {
$this -> string = $string;
}
// проверяет, начинается ли строка с заданной подстроки
function startsWith( $prefix ) {
$prefix = str_replace('/','\/',$prefix);
return preg_match("/$prefix$/",$this -> string);
}
// проверяет, заканчивается ли строка заданной подстрокой
function endsWith( $suffix ) {
$suffix = str_replace('/','\/',$suffix);
return preg_match("/$suffix$/",$this -> string);
}
// вырезает из строки часть подстроки
function substr( $begin, $length ) {
return substr( $this -> string, $begin, $length );
}
// возвращает строку без префикса
function withoutPrefix( $string ) {
$this -> string = substr_replace( $this -> string, '', 0, strlen($string) );
return $this;
}
// возвращает строка без суффикса
function withoutSuffix( $string ) {
$this -> string = substr_replace( $this -> string, '', -strlen($string) );
return $this;
}
// замена в строке
function replace( $from, $to ) {
return str_replace( $from, $to, $this -> string );
}
function contains($str) {
$str = str_replace('/','\/',$str);
return preg_match("/$str/",$this -> string);
}
function str() {
return $this -> __toString();
}
function __toString() {
return $this -> string;
}
}
$str = strs('\\MySite\\Controllers\\ArticleController.php'); // Создаем экземпляр класса Strings
print $str -> withoutPrefix('\\MySite'); // \Controllers\ArticleController.php
print $str -> contains('.php') ? 'Есть' : 'Нет'; // Есть
print $str -> endsWith('.php') ? 'Есть' : 'Нет'; // Есть
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment