Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:12:15+00:00 2026-06-12T09:12:15+00:00

I want to establish a generic sanitizer for my data that comes from various

  • 0

I want to establish a generic sanitizer for my data that comes from various sources. With sanitizing I mean (at this stage) applying htmlspecialchars to strings. Now, the data that comes from these sources can be anything from an object to an array to a string, all nested (and complicated), and the format is always a bit different.

So I thought of a recursive htmlspecialchars function that applies itself to arrays and objects, and only applies htmlspecialchars to strings, but how do I walk an object recursively?

Thanks.

EDIT: I think I should have mentioned this – I am actually building a RIA that relies heavily on JS and JSON for client-server communication. The only thing the server does is fetching stuff from the database and returning it to the client via JSON, in the following format:

{"stat":"ok","data":{...}}

Now as I said, data could be anything, not only coming from a DB in the form of strings, but also coming from an XML
The workflow to process the JSON is as follows:

  1. Fetch data from the DB/XML (source encoding is iso-8859-1)
  2. Put them into the “data” array

  3. Recursively convert from iso-8859-1 to utf-8 using

    private function utf8_encode_deep(&$input) {
    if (is_string($input)) {
        $input = $this -> str_encode_utf8($input);
    } else if (is_array($input)) {
        foreach ($input as &$value) {
            $this -> utf8_encode_deep($value);
        }
    
        unset($value);
    } else if (is_object($input)) {
        $vars = array_keys(get_object_vars($input));
    
        foreach ($vars as $var) {
            $this -> utf8_encode_deep($input -> $var);
        }
    }
    }
    
  4. Use PHP’s json_encode to convert the data into JSON

  5. Send (echo) the data to the client

  6. Render the data using JS (e.g. putting into a table)

And somewhere in between that, the data should be somehow sanitized (at this stage only htmlspecialchars). Now the question should have been: Where to sanitize, using what method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:12:15+00:00Added an answer on June 12, 2026 at 9:12 am

    You would only want to escape when outputting into HTML. And you cannot output a complete array or object into HTML, so escaping everything seems invalid.

    You have one level of indirection because of your JSON output. So you cannot decide in PHP what context the data is used for – JSON is still plain text, not HTML.

    So to decide whether any data inside the JSON must be escaped for HTML we must know how your Javascript is using the JSON data.

    Example: If your JSON is seen as plain text, and contains something like <b>BOLD</b>, then the expected outcome when used inside any HTML is exactly this text, including the chars that look like HTML tags, but no bold typesetting. This will only happen if your Javascript client handles this test as plain text, e.g. it DOES NOT use innerHTML() to place it on the page, because that would activate the HTML tags, but only innerText() or textContent() or any other convenience method in e.g. jQuery (.text()).

    If on the other hand you expect the JSON to include readymade HTML that is fed into innerHTML(), then you have to escape this string before it is put into JSON. BUT you must escape the whole string only if you do not want to add any formatting to it. Otherwise you are in a situation that uses templates for mixing predefined formatting with user content: The user content has to be escaped when put into HTML context, but the result must not – otherwise Javascript cannot put it into innerHTML() and enable the formatting.

    Basically a global escaping for everything inside your array or object most likely is wrong, unless you know for everything that it will be used in a HTML context by your Javascript.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to establish some fixed rows in head of the datatable. This is
I want to send data from a c# application to a c++ application through
I am trying to establish an abstract class. I want to ensure that any
I want to establish a standard script file that is imported into python at
I want to write a test to establish that an Exception is not raised
I want to establish a connection to OLAP Cube deployed on SSAS from a
I want to establish a connection between my local machine and MySQL database server
I have some java-app, and i want to establish a connection to some https
I am building DB class, in the constructor I want to establish the connection
Want to run javascript function from parent window in child window Example I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.