Forum Moderators: coopster
$value = array_map('htmlentities', $value, ENT_NOQUOTES);
After viewing repeated error messages stating that array_map expected parameter 3 to be an array, I decided to give it one and every one of my tests, using the following code, was then a success.
$attrib[0] = ENT_NOQUOTES;
$value = array_map('htmlentities', $value, $attrib);
However, in production, the same script failed as such.
function quote_smart($data) {
if (is_array($data)) { // Trim & htmlentities
$data = array_map('trim', $data);
$attrib[] = ENT_NOQUOTES;
$data = array_map('htmlentities', $data, $attrib);
echo '<br />l_id: ' . $data['l_id']; // normally should return username but for some reason return's "" instead;
// Stripslashes
if (get_magic_quotes_gpc()) {
$data = array_map('stripslashes', $data);
}
else {
$data = array_map('addslashes', $data);
}
// Quote if not a number or a numeric string
if (!array_map("is_numeric", $data)) {
$data = array_map("mysql_real_escape_string", $data);
}
}
else {
// Trim & htmlentities
$data = trim($data);
$data = htmlentities($data, ENT_NOQUOTES);
// Stripslashes
if (get_magic_quotes_gpc()) {
$data = stripslashes($data);
}
else {
$data = addslashes($data);
}
// Quote if not a number or a numeric string
if (!is_numeric($data)) {
$data="'" . mysql_real_escape_string($data) . "'";
}
}
return $data;
}
Help please before I go insane!
If the arrays are of unequal length, the shortest one will be extended with empty elements.
In other words (using [] as shorthand for array()):
array_map('html_entities', [x, y, z], [ENT_QUOTES]); html_entities(x, ENT_QUOTES);
html_entities(y, null);
html_entities(z, null); Unfortunately, PHP's support for functional programming is sorely lacking. But this is a fairly simple case--all you need is to create a new function which calls html_entities:
function ehtml($x) { return html_entities($x, ENT_QUOTES); }
// ...
array_map('ehtml', [x, y, z]); array_map([url=http://php.net/create_function]create_function[/url]('$x', 'return html_entities($x, ENT_QUOTES);'), [x, y, z]);
My problem is that the results I am getting are not what are expected. For example, please examine the following code fragment.
- test.php -
$value[0] = '<a href="test.php">"home"</a>';
$value[1] = '<a href="test.php">"home"</a>';$attrib[0] = ENT_NOQUOTES;
$attrib[1] = ENT_QUOTES;
echo "<br />before 1: " . $value[0];
echo "<br />before 2: " . $value[1];
$value = array_map('htmlentities', $value, $attrib);
echo "<br />after 1: " . $value[0];
echo "<br />after 2: " . $value[1];
Document -
before 1: "home"
before 2: "home"
after 1: <a href="test.php">"home"</a>
after 2: <a href="test.php">"home"</a>Source -
<br />
before 1: <a href="test.php">"home"</a><br />
before 2: <a href="test.php">"home"</a><br />
after 1: <a href="test.php">&quot;home&quot;</a><br />
after 2: <a href="test.php">&quot;home&quot;</a>
// $_POST = $l_id and $p_word as typed by the user in login.
// $data = $_POST and is passed to quote_smart.function quote_smart($data) {
if (is_array($data)) {
// Trim & htmlentities
$data = array_map('trim', $data);
$attrib[0] = ENT_NOQUOTES;
$attrib[1] = ENT_NOQUOTES;
$data = array_map('htmlentities', $data, $attrib);
// Stripslashes
if (get_magic_quotes_gpc()) {
$data = array_map('stripslashes', $data);
}
else {
$data = array_map('addslashes', $data);
}
// Quote if not a number or a numeric string
if (!array_map("is_numeric", $data)) {
$data = array_map("mysql_real_escape_string", $data);
}
}
else {
// Trim & htmlentities
$data = trim($data);
$data = htmlentities($data, ENT_NOQUOTES);
// Stripslashes
if (get_magic_quotes_gpc()) {
$data = stripslashes($data);
}
else {
$data = addslashes($data);
}
// Quote if not a number or a numeric string
if (!is_numeric($data)) {
$data="'" . mysql_real_escape_string($data) . "'";
}
}
return $data;
}
function catenate ($x, $y = '!') { return $x . $y; }
$arr = ['cat' => 'hat', 'monkey' => 'poo'];
$args = ['ly', 'fy'];
print_r(array_map('catenate', $arr));
print_r(array_map('catenate', $arr, $args)); Notice the difference between the arrays returned by the first and the second array_map. Does that help explain the problem you're having?
That said, your function could be shortened quite a bit (untested, but should give you the general idea):
function quote_smart($data) { if (is_array($data)) { return array_map('quote_smart', $data); } else { // Trim & htmlentities
$data = htmlentities(trim($data), ENT_NOQUOTES); /* I'm fairly confident the else branch was a bug: it would end up inverting the effect of magic_quotes, so magic_quotes on meant off and off meant on. Presumably you only wanted to undo it being on. */
// Stripslashes
if (get_magic_quotes_gpc()) {
$data = stripslashes($data);
} // Quote if not a number or a numeric string
if (!is_numeric($data)) {
$data="'" . mysql_real_escape_string($data) . "'";
}
return $data;}}
$data = htmlentities(trim($data), ENT_NOQUOTES);
Warning: array_map() [function.array-map]: Argument #3 should be an array in \test\template\lib\quote_smart.inc on line 9Warning: array_map() [function.array-map]: Argument #2 should be an array in \test\template\lib\quote_smart.inc on line 16
Warning: array_map() [function.array-map]: Argument #2 should be an array in \test\template\lib\quote_smart.inc on line 20
Warning: array_map() [function.array-map]: Argument #2 should be an array in \test\template\lib\quote_smart.inc on line 21
Warning: Cannot modify header information - headers already sent by (output started at \test\template\lib\quote_smart.inc:9) in \test\template\admission\validate.php on line 42
function quote_smart($data) { if (is_array($data)) {
$data = array_map('quote_smart', $data);
}
else {
// Trim & htmlentities
$data = htmlentities($data, ENT_NOQUOTES);
// Stripslashes
if (get_magic_quotes_gpc()) {
$data = stripslashes($data);
}
// Quote if not a number or a numeric string
if (!is_numeric($data)) {
$data = mysql_real_escape_string($data);
}
}
return $data;
}