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

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

图片转代码服务由CSDN问答提供 功能建议

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

  $ array = array(); 
 $ array = $ _POST ['file_add']; 
 
 $ outString =''; 
 
 \  nforeach($ 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()。'”}}'); 
 
 //如果有dpe条目,发送消息到jquery 
 if(mysql_num_rows  ($ result)> 0){
 
 $ outString。= $ file。  ''; 
 // echo $ output;  
} 
} 
 nif($ outString){//如果此字符串中有数字,则表示存在重复错误
 $ error = array('opp'=>“错误”,'file'=>  ; $ outString);  // box将为您的Dialog Text提供TEXT 
 // IMPORTANT,更改JSON输出以使用Opperation(opp)在javascript中指示
 //是否有错误或不是
 $ output = json_encode($ 错误); 
 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.

    已采纳该答案
    打赏 评论
  • douguadao3883 2014-03-22 17:03

    Try:

    $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

    打赏 评论
  • dtrvzd1171 2014-03-22 17:04

    How about just applying it to each element:

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

相关推荐 更多相似问题