如何从PHP中的mysql返回的文本字段中删除回车?

时间:2010-10-10 05:10:10

标签: php mysql json jquery-plugins

我正在使用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";

}

&GT;

2 个答案:

答案 0 :(得分:0)

TRIM()(PHP和MySQL版本)不会删除所有回车符,只会在输入之前和之后删除空格。你可以用php或MySQL去掉回报。我知道如何在php中做到这一点:

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

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

顺便说一下,如果你有PHP 5.2,你不需要疯狂编写自己的JSON。您可以使用json_encode()

答案 1 :(得分:0)

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

  

此函数返回一个字符串   空白从一开始就被剥夺了   和结束没有第二个   参数,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