【发布时间】:2016-09-23 13:48:38
【问题描述】:
我正在尝试在 PHP 数组中获取 html。当我将变量打印出来时,表格的格式正确,但将其输出到数组时,我的 html 结束标签会添加反斜杠(例如:)。我认为 PHP 中的斜线不需要任何转义字符。当我print_r($table); 它在不添加反斜杠的情况下输出并在我的浏览器中正确输出表格但是当我将 html 表格添加到 PHP 数组然后将数组作为 JSON 对象输出时,它会将反斜杠添加到我的 html 结束标记,如图所示在我的浏览器下面的输出中。任何想法都会有所帮助。
输出:
{"data":{"success":"true","carriers":"1,2","table":"
CarrierId<\/th>
CarrierName<\/th>
1<\/td> UHC<\/td><\/tr>
2<\/td> BlueCross<\/td><\/tr><\/table>"}}
PHP:
<?php
// Show all information, defaults to INFO_ALL
//phpinfo();
/*environment:
OS: windows Server 2012 Standard build 9200
IIS: IIS version 8.0.9200.16384 --> Authentication - win auth ->enabled, Anonymou auth -->disabled, CGI -> Impersonate User = true
http://www.microsoft.com/web/downloads/platform.aspx
http://blogs.msdn.com/b/brian_swan/archive/2010/02/10/sql-server-driver-for-php-understanding-windows-authentication.aspx
SQL: - MS SQL server 10.5.1600
PHP: PHP version 5.4.26 for IIS, copy php_sqlsrv_54_ts.dll to C:\Program Files (x86)\PHP\v5.4\ext directroy from
http://www.iis.net/learn/application-frameworks/install-and-configure-php-on-iis/install-the-sql-server-driver-for-php
php.ini file add
extension=php_pdo_sqlsrv_54_ts.dll
extension=php_sqlsrv_54_ts.dll
*/
//Connect to SQL with Authentication
$serverName = "PFIT-00-lync-03"; //serverName\instanceName
$connectionInfo = array( "Database"=>"spicewebinsurance", "UID"=>"spicerest", "PWD"=>"Password1");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
//echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$tsql= "select convert(varchar,CarrierId) as CarrierId, CarrierName as CarrierName from dbo.Carrier ";
//print_r($tsql) ;
$stmt=sqlsrv_query($conn, $tsql);
$table = "<table> <th>CarrierId</th><th>CarrierName</th>";
// Create table body
if ($stmt) {
$rows = sqlsrv_has_rows( $stmt );
if ($rows === true)
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) )
{
$carriers[] = $row['CarrierId'];
$table .= "<tr><td>";
$table .= $row['CarrierId'];
$table .= "</td><td>";
$table .= $row['CarrierName'];
$table .= "</td></tr>";
}
$table .= "</table>";
$carriers=implode(',', $carriers);
$return=array(
"success"=>"true",
"carriers"=>$carriers,
"table"=>$table
);
}
else
{
$return['success']=false;
}
echo '{"data":'.json_encode($return).'}';
sqlsrv_free_stmt( $stmt);
?>
【问题讨论】:
标签: php html associative-array