Friday, 14 November 2014

Cutting string from left and right

<?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"
?>

0 comments:

Post a Comment