Jump to content

variable variables - function


Destramic

Recommended Posts

i'm trying to get strip/add slashes function working but its coming up with an error on this line...but im sure im doing the right thing...does anyone know where im going wrong please or even if this is possible

 

$function = "add"; // or strip
$value = {$function}slashes($value);

 

thank you destramic

Link to comment
https://forums.phpfreaks.com/topic/201177-variable-variables-function/
Share on other sites

sorry im now having an error...i was firstly just setting $function as strip but when setting it in function argument as below im getting the error:

Warning: Missing argument 2 for add_strip_slashes_deep()

strange

<?php
function add_strip_slashes_deep($value, $function)
{
if (function_exists($function . "slashes"))
{
	if (get_magic_quotes_gpc() && 
	    is_array($value))
	{
		$value = array_map("add_strip_slashes_deep", $value);
	}
	else if (is_string($value))
	{
		$value = call_user_func($function . 'slashes', $value);
	}
}
else
{
	echo "An error has occurred.<br />\n";
}

    return $value;	
}


// Example
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = add_strip_slashes_deep($array, "strip");

// Output
print_r($array);
?> 

You could replace:

 

$value = array_map("add_strip_slashes_deep", $value);

 

With:

 

$value = array_map("add_strip_slashes_deep", $value, array_fill(0, sizeof($value), $function));

 

or just:

 

foreach($value as &$element)
{
$element = add_strip_slashes_deep($element, $function);
}

Archived

This topic is now archived and is closed to further replies.

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