Jump to content

trim


Destramic

Recommended Posts

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

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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

*/
Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.