0

Okay... to make a long story short... here is my code...


<?php 

$con = mysql_connect($db_server_name,$db_username,$db_password);
if (!$con)
  {
  echo "0";
  }
mysql_select_db("" . $db_database_name . "", $con);
$result = mysql_query("SELECT * FROM sched_posts
WHERE user_id='$user_id'");

while($row = mysql_fetch_array($result))
  {
  $post_id = $row['ID'];
  $post_year = $row['post_year'];
  $post_month = $row['post_month'];
  $post_day = $row['post_day'];
  $post_hour = $row['post_hour'];
  $post_minute = $row['post_minute'];
  $post_privacy = $row['post_privacy'];
  $post_message = $row['post_message'];
echo "              {";
echo "                  id: " . $post_id . ",";
echo "                  title: ' " . $post_message . "',";
echo "                  start: new Date(" . $post_year . ", " . $post_month . "-1, " . $post_day . ", " . $post_hour . ", " . $post_minute . "),";
echo "                  allDay: false";
echo "              },";
  }
?>

When returning results, the post_message sometime's comes back with apostrophes in it. How can I get those results to appear as \' instead of just ' (in other words... with a backslash in front of it)?

PS.. I know some of this code looks unnecessary but please try to ignore that.... this is only setup this way for some testing that i am doing for facebook SDK results (for example, the identifiers inside of the WHILE statement).

The problem is, the returned apostrophes are causing the entire thing to go loopy... you know what i mean.

4

3 回答 3

1

如果您将所有这些“日期部分”列转换为时间戳,您可以简单地使用json_encode()

$ts = mktime($post_hour, $post_minute, 0, $post_month, $post_day, $post_year);

echo json_encode(array(
    'id' => $row['ID'],
    'title' => $row['post_message'],
    'start' => date('r', $ts), // <-- that's a string now
    'allDay' => false,
));

JavaScript 使用 rfc822 格式的日期没有问题。

于 2013-10-09T14:44:11.553 回答
0

json_encode()函数旨在生成 JSON 数据,但由于 JSON 是 JavaScript 的子集,因此它是生成动态字符串的最佳替代方案。这是一个使用示例:

<?php

$post_id = 314;
$post_message = <<<EOM
Jim "Big Boy" O'brian wrote:

<strong>Hi</strong>

EOM;
$post_year = 2013;
$post_month = 10;
$post_day = 9;
$post_hour = 17;
$post_minute = 4;

echo "{";
echo "    id: " . $post_id . ",";
echo "    title: " . json_encode($post_message) . ",";
echo "    start: new Date(" . $post_year . ", " . $post_month . "-1, " . $post_day . ", " . $post_hour . ", " . $post_minute . "),";
echo "    allDay: false";
echo "},";

...产生:

title: "Jim \"Big Boy\" O'brian wrote:\r\n\r\n<strong>Hi<\/strong>\r\n"

请注意,您必须省略周围的引号;该功能会为您添加它们。

于 2013-10-09T15:08:40.587 回答
0

要添加反斜杠,函数addslashes() 将为此工作:

http://php.net/manual/en/function.addslashes.php

要可靠地对 JSON 进行 100% 编码(尤其是对于此类您无法预测/预期某些值/输入的字段),最好使用 json_encode():

while($row = mysql_fetch_array($result))
{
  $post_id = $row['ID'];
  $post_year = $row['post_year'];
  $post_month = $row['post_month'];
  $post_day = $row['post_day'];
  $post_hour = $row['post_hour'];
  $post_minute = $row['post_minute'];
  $post_privacy = $row['post_privacy'];
  $post_message = $row['post_message'];
  $dateString = ''; // computed date string...
  echo json_encode(array("id"=>$post_id,"title"=>$post_message,"start"=>
  $dateString,"allDay"=>false));
}
于 2013-10-09T14:36:52.633 回答