0

I am confuse abot json encoding. I did it but some result is converting in JSON and some not.

     {
   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));
  } 

My data is which is not converting in 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"}]

May be problem is on / or \\ ?

but dont know.

please help me expert

viraptor
  • 33,322
  • 10
  • 107
  • 191
user1629977
  • 437
  • 1
  • 5
  • 21
  • 3
    Where are you using `json_encode`? I don't see it in the code you posted. – Amal Murali Sep 09 '13 at 15:12
  • 2
    Obligatory: you can't parse HTML with Regular Expressions http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags Tọ̟̙̮̙͈ny ̡͓̼͕t̟̫̰̩̞̗͔͠h̸͍̫̜͙͙̭͈e̯̲̣͓͢ ̥͕͕̬̱p̀O̜̟͈n̰͞y̻̭̪͠,͜ ̳̝̮͙ͅhę̜̼͈͇̱ ̤̘̰͡c͇̖̙o̯͔̼̖̱͟m̕e͍̯̻S̀ – Halcyon Sep 09 '13 at 15:13
  • Perhaps explaining why you're using `json_encode()` would help. Maybe there's a better way to do what you're currently trying to do? – Amal Murali Sep 09 '13 at 15:19
  • I am new in php and want just display data in json. but some data is converting and some not.so that i will call it by android application. – user1629977 Sep 09 '13 at 15:23
  • This code looks like a big list of things not to do in PHP. The `json_encode()` line is okay, but the rest is scary. Why are you using that regex to parse the string ("don't parse HTML with regex!")? Especially since the string itself isn't HTML?? Why has the string got escaped slashes? That's definitely not going to help. Oh, and as a bonus, please note that the `mysql_xxx()` functions are deprecated; you should consider switching to the PDO library for your DB code. – Spudley Sep 09 '13 at 15:23
  • regex was important for present data in correct json parsing. – user1629977 Sep 09 '13 at 15:33
  • please check my output data . it is not converting in JSON. i checked by crome jsonview. – user1629977 Sep 09 '13 at 15:35

1 Answers1

3

First off: You're converting HTML by regex. That is incredibly bad/stupid and WILL turn around and bite you in the rump.

That being said, since you ARE converting HTML and then json_encoding the resulting array, you'll get just HTML-in-a-string, which your browser is going to render, e.g.

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

echo json_encode($arr);

which will give you a raw JSON string of

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

Viewed in a browser in default html-rendering mode, you'll end up with

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

Note how the tags are "removed" - because the browser rendered them. Do a view source of your page, and you'll see your tags. Also note that the closing tags were NOT rendered, because the/` was escaped by JSON, turning them into "invalid" html tags.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • If i will not use it. what is other way? so that my all data will be convert into json in proper format. please check my output data . it is not converting in JSON. i checked by crome jsonview. – user1629977 Sep 09 '13 at 15:36
  • That **IS** json you're getting. You're getting exactly what json_encode() is outputting, which is valid JSON text. with few limits, json_encode() doesn't care what you feed it - it'll do its best to convert that input to a valid JSON output. – Marc B Sep 09 '13 at 15:37
  • yes it is validate . but i want to it parse in chrome in JSON format. I saw by JSON View of Chrome. – user1629977 Sep 09 '13 at 15:40
  • 2
    You're probably not telling chrome that you're sending over JSON. remember that JSON is just plain-text. Chrome is not going to try and GUESS if it's json or not. Try `header('Content-type: application/json');` before dumping the json data. – Marc B Sep 09 '13 at 16:18