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

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 に答える
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 に答える