Я написал эти две функции для левой и правой обрезки строки:
/**
* @param string $str Original string
* @param string $needle String to trim from the end of $str
* @param bool|true $caseSensitive Perform case sensitive matching, defaults to true
* @return string Trimmed string
*/
function rightTrim($str, $needle, $caseSensitive = true)
{
$strPosFunction = $caseSensitive ? "strpos" : "stripos";
if ($strPosFunction($str, $needle, strlen($str) - strlen($needle)) !== false) {
$str = substr($str, 0, -strlen($needle));
}
return $str;
}
/**
* @param string $str Original string
* @param string $needle String to trim from the beginning of $str
* @param bool|true $caseSensitive Perform case sensitive matching, defaults to true
* @return string Trimmed string
*/
function leftTrim($str, $needle, $caseSensitive = true)
{
$strPosFunction = $caseSensitive ? "strpos" : "stripos";
if ($strPosFunction($str, $needle) === 0) {
$str = substr($str, strlen($needle));
}
return $str;
}