Home > Enterprise >  is_array function executes strings as well
is_array function executes strings as well

Time:08-28

I'm trying to write a function for arrays that strips the backslashes. My problem is that I'm using the "is_array" function to verify if the input actually is an array but it seems that it executes numbers and strings as well.

The stripping part seems to work though, so it's the is_array part I can't figure out

I'm using the "array_map" function because the input array could consists of several arrays

What am I missing?

Thanks.

function strip_backslashes_array( $arr )
{
    $result = is_array($arr) === true ?
        array_map('strip_backslashes_array', $arr) :
        implode( "",explode("\\",$arr ));

    return $result;
}


$number = 5;
$text = 'Hey th\\\ere!';
$array = array("\\1","2\\",3,4);

$value1 = strip_backslashes_array($number);
echo $value1; 
//returns 5. I would expect it to be Null as it is NOT an array.


$value2 = strip_backslashes_array($text);
echo $value2; 
//returns "Hey there!" so it has stripped the string but I would expect Null as it is NOT an array.

echo '</br>';

$value3 = sanitize_strip_backslashes_array($array);
print_r($value3); 
//returns "Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )" so it has stripped slashes as expected because it is an array. 

CodePudding user response:

Your recursion logic will not work if you want to return null when is_array($arr) == false. You wil need to use a different function to perform the string manipulation.

function strip_backslashes_array( $arr )
{
    $result = is_array($arr) ?
        array_map('strip_backslashes_str', $arr) :
        null;

    return $result;
}
function strip_backslashes_str($str){
    if (is_array($str)) 
      return strip_backslashes_array($str);
    
    return implode( "", explode("\\", $str));
}

CodePudding user response:

First, you don't need is_array($arr) === true you can just say is_array($arr) as is_array will always return a boolean so

$result = is_array($arr) === true ?
    array_map('strip_backslashes_array', $arr) :
    implode( "",explode("\\",$arr ));

becomes

$result = is_array($arr) ?
    array_map('strip_backslashes_array', $arr) :
    implode( "",explode("\\",$arr ));

But that doesn't change functionality.

It's not clear to me what you expect the result to be? Show some examples of inputs and the actual and expected outputs:

Given x
Actual y
Expected z

Here is where testing will help you. Start unit testing your function you will quickly figure out what is and isn't working, for example here is a start:

<?php

use PHPUnit\Framework\TestCase;

function strip_backslashes_array($arr)
{
    $result = is_array($arr) ?
        array_map('strip_backslashes_array', $arr) :
        implode('', explode('\\', $arr));

    return $result;
}

final class StripBackslashesArrayTest extends TestCase
{
    public function testStripBackslashesArray(): void
    {
        $this->assertSame([], strip_backslashes_array([]));
        $this->assertSame('', strip_backslashes_array(''));

        // strings
        $this->assertSame('fizz', strip_backslashes_array('fizz'));
        $this->assertSame('fizz', strip_backslashes_array('f\\izz'));
        $this->assertSame('123', strip_backslashes_array(123));

        // arrays
        $this->assertSame(['fizz'], strip_backslashes_array(['fizz']));
        $this->assertSame(['fizz'], strip_backslashes_array(['f\\izz']));
        $this->assertSame(['fizz', 'buzz'], strip_backslashes_array(['f\\izz', 'b\\uzz']));
        $this->assertSame([['fizz'], ['buzz']], strip_backslashes_array([['f\\izz'], ['b\\uzz']]));
    }
}

See https://phpunit.de/documentation.html

  •  Tags:  
  • php
  • Related