0

私はjsonエンコーディングについて混乱しています。私はそれを行いましたが、一部の結果は JSON に変換され、一部はそうではありません。

     {
   while($row = mysql_fetch_assoc($result))
    {
        $row = preg_replace('%
    # Match an opening or closing HTML 4.01 tag.
    </?                  # Tag opening "<" delimiter.
    (?:                  # Group for HTML 4.01 tags.
      ABBR|ACRONYM|ADDRESS|APPLET|AREA|A|BASE|BASEFONT|BDO|BIG|
      BLOCKQUOTE|BODY|BR|BUTTON|B|CAPTION|CENTER|CITE|CODE|COL|
      COLGROUP|DD|DEL|DFN|DIR|DIV|DL|DT|EM|FIELDSET|FONT|FORM|
      FRAME|FRAMESET|H\d|HEAD|HR|HTML|IFRAME|IMG|INPUT|INS|
      ISINDEX|I|KBD|LABEL|LEGEND|LI|LINK|MAP|MENU|META|NOFRAMES|
      NOSCRIPT|OBJECT|OL|OPTGROUP|OPTION|PARAM|PRE|P|Q|SAMP|
      SCRIPT|SELECT|SMALL|SPAN|STRIKE|STRONG|STYLE|SUB|SUP|S|
      TABLE|TD|TBODY|TEXTAREA|TFOOT|TH|THEAD|TITLE|TR|TT|U|UL|VAR
    )\b                  # End group of tag name alternative.
    (?:                  # Non-capture group for optional attribute(s).
      \s+                # Attributes must be separated by whitespace.
      [\w\-.:]+          # Attribute name is required for attr=value pair.
      (?:                # Non-capture group for optional attribute value.
        \s*=\s*          # Name and value separated by "=" and optional ws.
        (?:              # Non-capture group for attrib value alternatives.
          "[^"]*"        # Double quoted string.
        | \'[^\']*\'     # Single quoted string.
        | [\w\-.:]+      # Non-quoted attrib value can be A-Z0-9-._:
        )                # End of attribute value alternatives.
      )?                 # Attribute value is optional.
    )*                   # Allow zero or more attribute=value pairs
    \s*                  # Whitespace is allowed before closing delimiter.
    /?                   # Tag may be empty (with self-closing "/>" sequence.
    >                    # Opening tag closing ">" delimiter.
    | <!--.*?-->         # Or a (non-SGML compliant) HTML comment.
    | <!DOCTYPE[^>]*>    # Or a DOCTYPE.
    %six', '', $row);

     $output[]=$row;
    }
    if($output) {
    print(json_encode($output));
  } 

私のデータは JSON に変換されていません。

[{"productid":"624","sku":"BBC-A-B-624","description":"
Burma Incoming Mail : Very Scarce & Attractive \\\"Third Burma War\\\" cover (and letters). 1886 Inbound cover from England to Mogaung, Upper Burma via Thayetmyo, Mandalay & Bhamo, franked with Great Britain QV. 5d tied by ST. JOHN\\'S WOOD duplex cancel (dtd JA 1 86). The cover from the \\\"Burton\\\" correspondence was addressed to \\\"Burton\\\" of the \\\"army Medical Staff \/ Station Hospital \/ Thayetmyo \/ B. Burmah\\\". SEA \/ POST OFFICE \/ D transit (dtd 7 JAN 86). The cover was received at Thayetmyo and bears small (thimble) pmk of THAYETMYO (dtd JA 26) and readdressed via the \\\"Steamer Ranpou\\\" to MANDALAY and cancelled by Square Circle THAYETMYO cancel (dtd JA 27 \/ 96). Three MANDALAY cancels (two types - dtd 5? & FEB 86). Readddressed to \\\"Bhamo\\\" and then to Mogaung. Manuscript \\\"Rec\\'d at MOGAUNG \/ 19 MAR 1886\\\". (We know that Burton was the Senior Medical Officer of 5\/B Field hospital which was served by Field P.O. No. 2. It is known that Burton was in Bhamo In March 1886 (see Davis & Martyn P. 77), but this cover shows that he went north to Mogaung where he received this cove on March 19th. Davis & Martin (P. 7) also mention the THAYETMYO Square Circle cancel found on this cover - it is a very scarce marketing.) The cover contains one short letter (from Burton\\'s mother) and a part of a second letter. The letter is quite interesting it that it states: \\\"I see this Monday that there is a Proclamation from the Queen Empress & that Burmah is annexed to the British Empire\\\" - a very significant event in the history of British Burma! F\/VF Condition.","price":"425","image":"sphil628.jpg","createddate":"2013-05-26","status":"Active","isordered":"0","view":"1"}]

/ または \\ に問題がある可能性がありますか?

しかし、知りません。

専門家を助けてください

4

1 に答える 1

3

まず、HTML を正規表現で変換しています。それは信じられないほど悪い/愚かであり、振り返ってあなたの尻に噛みつきます.

そうは言っても、HTML を変換してから、結果の配列を json_encoding しているので、ブラウザがレンダリングしようとしている HTML-in-a-string だけが得られます。

$arr = array(
    '<p>This is a paragraph</p>',
    '<div>this div has an <span>in-line span</span></div>'
);

echo json_encode($arr);

これにより、生のJSON文字列が得られます

["<p>This is a paragraph<\/p>","<div>this div has an <span>in-line span<\/span><\/div>"]

デフォルトの HTML レンダリング モードでブラウザで表示すると、最終的に次のようになります。

["
This is a paragraph<\/p>
","
This div has an in-line span<\/span><\/div>"]

タグがどのように「削除」されたかに注意してください。これは、ブラウザーがタグをレンダリングしたためです。view source of your page, and you'll see your tags. Also note that the closing tags were NOT rendered, because the/` が JSON によってエスケープされ、「無効な」html タグに変換されました。

于 2013-09-09T15:24:24.077 に答える