0

我对abot 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 回答