【问题标题】:How to strip carriage returns from text fields returned from mysql in PHP?如何从 PHP 中从 mysql 返回的文本字段中去除回车?
【发布时间】:2010-10-10 05:10:09
【问题描述】:

我正在使用 PHP 从 MYSQL 读取文本字段 (DESCRIPTION) 并准备一个 JSON 对象。以 JQUERY DataTables Plug-In 示例为蓝本。

DESCRIPTION 字段包含回车,我相信这会导致 JSON 无效。 JSONLINT.com 产生“语法错误,第 35 行出现意外的 TINVALID。解析失败”。

http://ageara.com/exp3/server_processing_details_col.php 应该返回有效的 JSON

我正在尝试使用 PHP TRIM() 函数来删除 CR,但运气不佳。

相关 PHP 代码如下 ....

$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());

$sQuery = "
    SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];

$sQuery = "
    SELECT COUNT(TITLE)
    FROM   geometa_small
";
$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];

/* write the JSON output */
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
while ( $aRow = mysql_fetch_array( $rResult ) )
{ 
    $sOutput .= "[";  
    $sOutput .= '"<img src=\"details_open.png\">",';
    $sOutput .= '"'.str_replace('"', '\"', $aRow['TITLE']).'",';
    $sOutput .= '"'.str_replace('"', '\"', $aRow['DATA_CUSTODIAN_ORGANIZATION']).'",';
    $sOutput .= '"'.str_replace('"', '\"', $aRow['RECORD_TYPE']).'",';
    $sOutput .= '"'.str_replace('"', '\"', $aRow['RESOURCE_STATUS']).'",';
    $sOutput .= '"'.str_replace('"', '\"', $aRow['RESOURCE_STORAGE_LOCATION']).'",';
    $sOutput .= '"'.str_replace('"', '\"', $aRow['UNIQUE_METADATA_URL']).'",';
    $sOutput .= '"'.trim(str_replace('"', '\"', $aRow['DESCRIPTION']), "/r").'"';
    $sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );

$sOutput .= '] }';
echo $sOutput;

function fnColumnToField( $i )
{
    /* Note that column 0 is the details column */
    if ( $i == 0 ||$i == 1 )
        return "TITLE";
    else if ( $i == 2 )
        return "DATA_CUSTODIAN_ORGANIZATION";
    else if ( $i == 3 )
        return "RECORD_TYPE";
    else if ( $i == 4 )
        return "RESOURCE_STATUS";
    else if ( $i == 5 )
        return "RESOURCE_STORAGE_LOCATION";
    else if ( $i == 6 )
        return "UNIQUE_METADATA_URL";
    else if ( $i == 7 )
        return "DESCRIPTION";

}

?>

【问题讨论】:

    标签: php mysql json jquery-plugins


    【解决方案1】:

    TRIM()(PHP 和 MySQL 版本)不会删除所有回车符,只会删除条目前后的空格。您可以使用 php 或 MySQL 去除返回值。我知道如何在 php 中做到这一点:

    str_replace(array("\r", "\n"), array('', ''), $mysqldata);
    

    希望这会有所帮助。不是 100% 确定这是否是 JSON 无效的原因。

    顺便说一句,如果您有 PHP 5.2,则无需发疯并编写自己的 JSON。你可以使用json_encode()

    【讨论】:

    • str_replace(array("\r", "\n"), array('', ''), $sOutput);回声 $s 输出;但没有工作会调查 json_encode()
    • 好答案,更进一步,您可以将代码更改为 str_replace( array( "\r" , "\n" ) ,'\n' , $mysqldata );,因为 '\n' 将不再被视为 PHP 中的换行符(它只会被视为两个字符 - 一个 ' \' 和一个 'n'),但是当 javascript 在另一边解析它时,它会将它们视为换行符。 (此外,您可以将多个字符串替换为单个字符串,方法是将数组用于第一个变量,将单个字符串用作第二个变量,str_replace()。)
    • 不要用 '\n' 代替,那样不好!但我不知道你可以用一个字符串替换数组。感谢您指出这一点。
    • 感谢有关 str_replace() 与 trim() 的建议以及使用数组的选项 - 不幸的是,从 JSON 输出中删除 仍然不成功。尝试使用此行 ...$sOutput .= '"'.str_replace('\r\n','XXX', $aRow['DESCRIPTION']).'"';代替 $sOutput .= '"'.trim(str_replace('"', '\"', $aRow['DESCRIPTION']), "/r").'"';没有任何双引号可以转义。
    • 单引号中的\r\n和双引号中的不一样。
    【解决方案2】:

    Trim 会根据手册删除 \r 和 \n:

    这个函数返回一个字符串 从一开始就去掉空格 和 str 的结尾。没有第二个 参数, trim() 将删除这些 字符:

    * " " (ASCII 32 (0x20)), an ordinary space.
    * "\t" (ASCII 9 (0x09)), a tab.
    * "\n" (ASCII 10 (0x0A)), a new line (line feed).
    * "\r" (ASCII 13 (0x0D)), a carriage return.
    * "\0" (ASCII 0 (0x00)), the NUL-byte.
    * "\x0B" (ASCII 11 (0x0B)), a vertical tab.
    

    http://www.php.net/trim

    【讨论】:

      猜你喜欢
      • 2011-05-04
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 2021-04-19
      相关资源
      最近更新 更多