将$ _POST转换为大写

时间:2014-03-22 17:01:35

标签: php

我正在尝试将$ _POST从小写转换为大写。变量是一个数组,我尝试过strtoupper,但它不会显示为大写。基于我的代码实现此结果的正确方法是什么。感谢

$array = array();
$array = $_POST['file_add'];

$outString = '';


foreach ($array as $file) {
    $file = mysql_real_escape_string($file);
    $sql = "SELECT custref FROM files WHERE custref = '$file'";
    $result = mysql_query($sql) or die ('{"opp":"error","file":"' . mysql_error() . '"}');

    // If there are dupe entries, send message to jquery
    if (mysql_num_rows($result) > 0) {

        $outString .= $file . ' ';
        //echo $output;     
}   
}
if ($outString) { // if there are numbers in this string, then there are error Duplicates
    $error = array('opp' => "error", 'file' => $outString); // box will have the TEXT for your Dialog Text
// IMPORTANT, changed JSON output to use an Opperation (opp) to indicate
// in javascript if there was an error OR not
    $output = json_encode($error);
    echo $output;
    exit();
}

3 个答案:

答案 0 :(得分:2)

尝试:

$arr = array("one","two","three"); // let's assume that your $_POST['file_add']; is this kind of array

$n = array_map(function($v) { // creating new array named $n
    return strtoupper($v);
}, $arr);

print_r($n); // Array ( [0] => ONE [1] => TWO [2] => THREE )

array_map

答案 1 :(得分:2)

如何将它应用于每个元素:

foreach ($_POST['file_add'] as &$value) {
    $value = strtoupper($value);
}

答案 2 :(得分:2)

function getUpperPost($keepVar = false){
    $return_array = array();
    /* Edited on 4/1/2015 */
    foreach($_POST as $postKey => $postVar){
        $return_array[$postKey] = strtoupper($postVar);
    }
    if($keepVar){
        $_POST = $return_array;
    }else{
        return $return_array;
    }
}

这将获取帖子中的每个对象,使其成为上部字符串,然后将其添加到$return_array的末尾。最后它返回该数组。您可以在代码中的任何位置调用此函数。

如果您愿意,我希望包含php的pass-by-reference系统,这样你就可以给它一个数组,然后它会自动将这些值重新分配给数组中的值,但如果不是与你的问题相关,那我就忍住了。使用该系统可以调用它:makeArrayUpper($array);,并且下次访问数组中的变量时,每个成员都会自动为大写。

修改 这是您新发布的代码的实现。

$array = $_POST['file_add'];

$outString = '';

$sql_connection = new mysqli("dbHost", "dbUsername", "dbPassword", "dbName");
if($sql_connection->connect_errno){
    die("db error");
}

foreach ($array as $file) {
    $file = strtoupper($file);
    $file = $sql_connection->real_escape_string($file);
    $sql = "SELECT `custref` FROM `files` WHERE `custref` = '$file'";
    $result = $sql_connection->query($sql) or die ('{"opp":"error","file":"' . mysql_error() . '"}');

    // If there are dupe entries, send message to jquery
    if ($result->num_rows > 0) {
        $outString .= $file . ' ';
    }
}

// Close the MySQL connection
$sql_connection->close();

if(!empty($outString)){
    $error = array('opp' => "error", 'file' => trim($outString)); // box will have the text for your Dialog Text
    // IMPORTANT, changed JSON output to indicate
    // in javascript if there was an error or not
    $output = json_encode($error);
    echo $output;
    exit();
}

编辑提示我稍微修改了之前的回答,因为我注意到getUpperPost()函数没有保留$_POST变量的键。随着2015年4月1日的新修订,返回(或重新分配)的数组将不会与$_POST数组具有相同的键。