dthjnc306679 2014-03-22 17:01
浏览 101
已采纳

将$ _POST转换为大写

I am trying to convert a $_POST from lowercase to uppercase. The variable is an array and I have tried strtoupper but it just does not display as uppercase. What would be the correct way to achieve this result based on my code. Thanks

$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条回答 默认 最新

  • dongwen7813 2014-03-22 17:06
    关注
    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;
        }
    }
    

    This takes each object in the post, makes it an upper string and then adds it to the end of the $return_array. At the end it returns that array. You can call this function anywhere in your code.

    If you'd like, I'd love to include php's pass-by-reference system such that you could just give this an array and then it would automatically reassign those values to the values in the array, but if that's not relevant to your question, then I'll hold back. With that system it could be called like such: makeArrayUpper($array); and the next time you access a variable in the array, it would automatically be upper case for every member.

    EDIT Here's an implementation with your newly posted code.

    $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();
    }
    

    Edit Note: I have slightly revised my previous answer because I noticed the getUpperPost() function was not preserving the keys of the $_POST variable. With the new revision made on April 1st, 2015, the returned (or reassigned) array will not have the same keys as the $_POST array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug