<?php
class
TrimFilter{
private
$options
=
array
();
private
$defaults
=
array
(
'character_mask'
=>
" \t\n\r\0\x0B"
);
public
function
__construct(
array
$options
=
array
()){
$this
->options =
$options
;
}
private
function
get_options(
array
$defaults
){
return
array_merge
(
$defaults
,
$this
->options);
}
function
trimString(
$value
){
$ops
=
$this
->get_options(
$this
->defaults);
if
(key_exists(
'character_mask'
,
$ops
)){
return
trim(
$value
,
$ops
[
'character_mask'
]);
}
return
trim(
$value
);
}
function
ltrimString(
$value
){
$ops
=
$this
->get_options(
$this
->defaults);
if
(key_exists(
'character_mask'
,
$ops
)){
return
ltrim(
$value
,
$ops
[
'character_mask'
]);
}
return
ltrim(
$value
);
}
function
rtrimString(
$value
){
$ops
=
$this
->get_options(
$this
->defaults);
if
(key_exists(
'character_mask'
,
$ops
)){
return
rtrim(
$value
,
$ops
[
'character_mask'
]);
}
return
rtrim(
$value
);
}
}
$trim_args
=
array
(
'options'
=>
array
(
new
TrimFilter(
array
(
'character_mask'
=>
" a"
)),TRIMSTRING
)
);
$ltrim_args
=
array
(
'options'
=>
array
(
new
TrimFilter(
array
(
'character_mask'
=>
" a"
)),LTRIMSTRING
)
);
$rtrim_args
=
array
(
'options'
=>
array
(
new
TrimFilter(),RTRIMSTRING
)
);
$str
=
"abcd "
;
var_dump(filter_var(
$str
,FILTER_CALLBACK,
$trim_args
));
var_dump(trim(
$str
,
" a"
));
var_dump(filter_var(
$str
,FILTER_CALLBACK,
$ltrim_args
));
var_dump(ltrim(
$str
,
" a"
));
var_dump(filter_var(
$str
,FILTER_CALLBACK,
$rtrim_args
));
var_dump(ltrim(
$str
));
?>