Json编码无法正常工作

时间:2013-09-09 15:11:32

标签: php json

我混淆了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"}]

可能是问题在/或\\?

但不知道。

请帮助我专家

1 个答案:

答案 0 :(得分: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>"]

注意标签是如何“删除”的 - 因为浏览器会渲染它们。 JSON转义了view source of your page, and you'll see your tags. Also note that the closing tags were NOT rendered, because the /`,将它们转换为“无效的”html标记。

相关问题