Skip to content

Instantly share code, notes, and snippets.

@rafasashi
Last active August 29, 2015 14:19
Show Gist options
  • Save rafasashi/c918b48c15a0b333fb8d to your computer and use it in GitHub Desktop.
Save rafasashi/c918b48c15a0b333fb8d to your computer and use it in GitHub Desktop.
function custom_trim($str,$trim=''){
if($trim==''){
$str=trim($str);
}
if($trim==' '){
$str=preg_replace('/^\p{Z}+|\p{Z}+$/u', '', $str);
}
else{
$str=custom_ltrim($str,$trim);
$str=custom_rtrim($str,$trim);
}
return $str;
}
function custom_ltrim($str,$trim){
if(is_array($trim)){
foreach($trim as $t){
$str=custom_rtrim($str,$t);
}
}
else{
if(custom_strlen($trim)==1){
$str=ltrim($str,$trim);
}
else{
$delim="`";
//$patern="(".$trim.")(.*)";
$patern="^(".$trim.")?(.*)$";
$str=preg_replace($delim.$patern.$delim."i", "$2", $str);
}
}
return $str;
}
function custom_rtrim($str,$trim){
if(is_array($trim)){
foreach($trim as $t){
$str=custom_rtrim($str,$t);
}
}
else{
if(custom_strlen($trim)==1){
$str=rtrim($str,$trim);
}
else{
$delim="`";
//$patern="(".$trim.")(.*)";
$patern="^(.*)".$trim."$";
$str=preg_replace($delim.$patern.$delim."i", "$1", $str);
}
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment