2
PHP配列でhtmlを取得しようとしています。変数を出力するとき、テーブルは正しい形式ですが、配列に出力されるので、HTML終了タグ(例:</th>)にバックスラッシュが追加されます。私はPHPでスラッシュのためのエスケープ文字が必要だと思っていませんでした。私はprint_r($table);
バックスラッシュを追加せずに出力し、ブラウザに正しくテーブルを出力しますが、htmlテーブルをPHP配列に追加して配列をJSONオブジェクトとして出力すると、HTML終了タグにバックスラッシュが追加されます私のブラウザから下に出力します。どんな考えが役に立つでしょう。json_encode()エスケープ/ with
出力:
{"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でエスケープ文字を探していただけで、json_encodeでは見つからなかった。 – user6382877