Jump to content

array_map() probably obvious mistake


bleuh111

Recommended Posts

The code below is part of a class to escape strings, but should also accept an array, using array_map() to do the job. Unfortunately, passing an array results in the original, unescaped array being returned - can't figure out why? If you uncomment the echo statement, it is outputting the correct escaped string for each element of the array, so the callback is definitely happening.

 

  public function escape_str($str)

  {

      if (is_array($str))

      {

        array_map(array('MYSQL_DB','escape_str'),$str);

      }

      else

      {

        if (get_magic_quotes_gpc()) {

            $str = stripslashes($str);

        }

        if (!is_numeric($str)) {

            $str = "'" . mysql_real_escape_string($str) . "'";

            //echo $str.'<br />';

        }

      }

      return $str;

  }

 

Example:

$xt = array("' OR ''=''",'"abcdefg"');

print_r($db->escape_str($xt));

 

Outputs original array, not escaped!

 

Can anyone help with this? Thanks.

Link to comment
Share on other sites

You're not assigning the return value of the array_map function to anything. If you take a look at the manual page (array_map) you'll see the function returns the array after having applied the supplied function to all the elements of the array.

 

This is something you have to keep an eye on with the array function; some do their job in-place, others create a copy.

Link to comment
Share on other sites

You're not assigning the return value of the array_map function to anything. If you take a look at the manual page (array_map) you'll see the function returns the array after having applied the supplied function to all the elements of the array.

 

This is something you have to keep an eye on with the array function; some do their job in-place, others create a copy.

 

This is a Home Simpson "D'oh!" moment!

I knew it was something bloody obvious!

 

$str = array_map(.....) is of course the correct syntax.

 

I did know that...honest....just having a bad day due to stomach flu!

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.