2

I am trying to get html in a PHP array. When I print the variable out the table is in the correct format but be it is outputted to the array there are added backslashes to my html closing tags (Example: </th>). I didnt think there needed to be any escape characters for slash in PHP. When I print_r($table); it outputs without adding the backslash and outputs the table correctly in my browser but when I add the html table to a PHP array and then output the array as a JSON object it adds the backslash to my html closing tag as shown in the output below from my browser. Any thoughts would be helpful.

Output:

{"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);
?>
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87

1 Answers1

0

I haven't read up on why the / is escaped but use option JSON_UNESCAPED_SLASHES:

echo '{"data":'.json_encode($return, JSON_UNESCAPED_SLASHES).'}';
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • **Thank you so much!** That worked. I really appreciate your help! i was only looking up escape characters in PHP and not for json_encode. – user6382877 May 27 '16 at 19:21