Destramic Posted May 30, 2015 Share Posted May 30, 2015 hey guys I'm having a but of trouble when it comes to using the trim function...for some reason when using it in this method it doesn't return the right results...can anyone tell me where im going wrong please? <?php class Trim { public function __invoke($data, $character_mask = null) { if (is_array($data)) { array_walk_recursive($data, array($this, '__invoke')); } else { $data = trim($data, $character_mask); } return $data; } } $trim = new Trim; $string = $trim('this a test .', 't'); $array = $trim(array('this is a string .', ' test two'), 't'); echo $string; print_r($array); echo trim('yesssss', 's'); results his a test . Array ( [0] => this is a string . [1] => test two ) ye thank you Quote Link to comment https://forums.phpfreaks.com/topic/296546-trim/ Share on other sites More sharing options...
Ch0cu3r Posted May 30, 2015 Share Posted May 30, 2015 (edited) Its not working for your second test because array_walk_recursive passes the key/value pairs as the 1st and 2nd arguments to the callback function. Also the value for the $character_mask will not be passed to the callback function either. You will need to pass it as the 3rd argument to array_walk_recursive, the character mask will then be available as the 3rd argument to the callback function. Also why does the trim routine need to be wrapped in a class? It would be better to do your trim routine within a function. Edited May 30, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/296546-trim/#findComment-1512851 Share on other sites More sharing options...
Barand Posted May 30, 2015 Share Posted May 30, 2015 You also need to pass the first argument to __invoke() by reference otherwise the callback works but on a copy of the array. class Trim { public function __invoke(&$data, $dummy, $character_mask = null) { if (is_array($data)) { array_walk_recursive($data, array($this, '__invoke'),$character_mask); } else { $data = trim($data, $character_mask); } return $data; } } $string = 'this a test .'; $array = array('this is a string .', ' test two'); $trim = new Trim; $string = $trim($string, null, 't'); $array = $trim($array, null, 't'); echo '<pre>'.$string; print_r($array); echo trim('yesssss', 's'); /* OUTPUTS his a test . Array ( [0] => his is a string . [1] => test two ) ye */ Quote Link to comment https://forums.phpfreaks.com/topic/296546-trim/#findComment-1512853 Share on other sites More sharing options...
Destramic Posted May 30, 2015 Author Share Posted May 30, 2015 thanks guys i made a few changes to the way it functions...thought it would be easier to se thet character mask in constructor or by method... <?php namespace Filter class Trim { protected $_character_mask; public function __construct($character_mask = null) { $this->set_character_mask($character_mask); } public function filter(&$data, $dummy = null) { if (is_array($data)) { array_walk_recursive($data, array($this, 'filter')); } else { $character_mask = $this->get_character_mask(); $data = trim($data, $character_mask); } return $data; } public function get_character_mask() { return $this->_character_mask; } public function set_character_mask($character_mask) { $this->_character_mask = $character_mask; } } $string = 'this a test .r'; $array = array('this is a stringo .o', ' test two'); $trim = new Trim('r'); $string = $trim->filter($string); $trim->set_character_mask('o'); $array = $trim->filter($array); echo '<pre>'.$string; print_r($array); echo trim('yesssss', 's'); the reason i use class instead of functions cause i use it with my framework... i can just do this due to my auto-loader use Filter\Trim as Trim $trim = new Trim; $trim->filter($post); i never use function to be honest... Quote Link to comment https://forums.phpfreaks.com/topic/296546-trim/#findComment-1512867 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.