php - Trim leading and trailing white spaces in JSON values -


All,

I request JSON to use PHP on a web server and it Gives me a JSON Feedback in a variable will have many keys and value in the JSON response I would like to have a function which places white space behind and behind in each "value" of the key-value pair for the whole JSON response.

How can I do this via PHP?

  {"glossary": {"title": "example terminology", "glossdiv": { "S": "SGML", "Sorts": "SGML", "GlossTerm": "Standard Generalized Markup Language ‡ flfi ..", "Title": "S", "Glossy": {"Gloss Anti" "Code": "SGML", "Abbreave": "ISO 8879: 1986", "Gloss Def": {"Paragraph": "Make Markup Languages ​​Like DokBook", "Gloss Sizes": ["GMA "Mark")}}}}  

Before processing the data it is encoded in JSON format. It is better to clear the source from the mess with the JSN version and possibly break the syntax with the removal of a distorted regazx.

Actually, do this:

  Forex currency ($ data as $ key = & gt; $ value) {$ data [$ of] = trim ( $ Value); } $ Json = json_encode ($ data); // $ json values ​​are now pre / post-white space  

(assuming this is a simple 1-dimensional array).

Follow the edit / comment:

Does your PHP script bring this external JSON? If this is the case, you can decode JSON in PHP Object / Arrays, trim the white space, and then convert it to json again in the encoding:

  $ Json = get_json_from_external_source (); $ Data = json_decode ($ json);  

and then foreach loop (or Arame_map as mentioned in Tomelic's comment) as if it were earlier if you are limiting this client-side in javascript, It is necessary that you can make equivalent processing before handing it over.

Edit / Comment Followup # 2:

I am very doubtful that these are behind the values ​​behind the JSON data. JSON is fully capable of handling places within the string and it does not care how many (or some) are. It is most likely that this glossitum entry has the possibility of characters with funky.

If you are on PHP 5.3 (or higher beta version), then it will be reported why the decode is unsuccessful.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -