PHP: Cutting string from left and right

strltrim and strrtrim functions solve string trimming problem

function that cuts left part of string

function strltrim($string, $part) {
  $string = trim($string);
  if (substr($string, 0, strlen($part))==$part)
    return(substr($string, strlen($part)));
  else
   return $string; 
}

function that cuts right part of string

function strrtrim($string, $part) {
  $string = trim($string);
  if (substr($string, -strlen($part))==$part)
    return(substr($string, 0, strlen($string)-strlen($part)));
  else
   return $string; 
}

Usage:

$myname = "John Miller";
 
echo strltrim($myname,"John ");  //result "Miller" 
 
echo strrtrim($myname," Miller"); //result "John"

Full Script:

<?php
function strltrim($string, $part) {
  $string = trim($string);
  if (substr($string, 0, strlen($part))==$part)
    return(substr($string, strlen($part)));
  else
   return $string; 
}
 
function strrtrim($string, $part) {
  $string = trim($string);
  if (substr($string, -strlen($part))==$part)
    return(substr($string, 0, strlen($string)-strlen($part)));
  else
   return $string; 
}
 
$myname = "John Miller";
 
echo strltrim($myname,"John ");  //result "Miller" 
 
echo strrtrim($myname," Miller"); //result "John"
?>