js

Showing posts with label Web Development. Show all posts
Showing posts with label Web Development. Show all posts

Monday, April 22, 2024

Javascript: Explain JSON.stringify() in Javascript

 Q. Explain JSON.stringify() in Javascript

 

JSON.stringify() is a method in JavaScript that converts a JavaScript object or value into a JSON string. JSON stands for JavaScript Object Notation, and it's a lightweight data-interchange format that's easy for humans to read and write and easy for machines to parse and generate.

Here's how JSON.stringify() works:

  1. If the input value is an object (including arrays, dates, and plain objects), JSON.stringify() will serialize it into a JSON string.
  2. If the input value is not an object (like a string, number, boolean, or null), it will be coerced into its corresponding primitive value, and that value will be returned.
  3. If the input value is undefined, a function, or a symbol, it will be excluded from the resulting JSON string (or replaced with null if it's a property value within an object).

Here's a simple example:

javascript
const obj = {
name
: "John",
age
: 30,
isAdmin
: true,
hobbies: ["reading", "coding", "hiking"],
address: { city: "New York", zip: "10001" }
};
const
jsonString = JSON.stringify(obj);
console.log(jsonString);

This will output:

Result
{"name":"John","age":30,"isAdmin":true,"hobbies":["reading","coding","hiking"],"address":{"city":"New York","zip":"10001"}}

You can also provide a "replacer" function or an array of property names to specify which properties should be included or how they should be transformed during serialization. Additionally, you can specify a "space" parameter to add whitespace for improved readability of the resulting JSON string.

javascript
const obj = {
name
: "John",
age: 30,
isAdmin: true,
hobbies
: ["reading", "coding", "hiking"],
address: { city: "New York", zip: "10001" }
};
const jsonString = JSON.stringify(obj, ['name', 'hobbies'], 2);
console.log(jsonString);

This will output:

Result
{ "name": "John", "hobbies": [ "reading", "coding", "hiking" ] }

This is particularly useful when you want to exclude certain properties or control the structure of the resulting JSON string.

PHP: Explain strip_tags() in PHP

 Q. Explain strip_tags() in PHP

In PHP, strip_tags() is a function used to remove HTML and PHP tags from a string. It's primarily used for sanitizing user input to prevent cross-site scripting (XSS) attacks by removing any potentially harmful HTML or PHP code before displaying the content to users.

Here's the basic syntax of strip_tags():

php
string strip_tags ( string $str [, string $allowable_tags ] )
  • $str: The input string from which HTML and PHP tags will be stripped.
  • $allowable_tags (optional): A string containing a list of tags that should not be removed. If specified, only the tags listed in this string will be preserved; all other tags will be removed.

If $allowable_tags is not provided, strip_tags() removes all HTML and PHP tags from the input string.

Example without $allowable_tags:

php
$input = '<p>Hello <b>world</b>!</p>';
$output = strip_tags($input);
echo $output;
// Output: Hello world!

In this example, <p> and <b> tags are removed from the input string, leaving only the text "Hello world!".

Example with $allowable_tags:

php
$input = '<p>Hello <b>world</b>! <a href="#">Click here</a></p>';
$output = strip_tags($input, '<b>');
echo $output;
// Output: Hello <b>world</b>!

In this example, only the <b> tag is preserved because it's listed in the $allowable_tags parameter. All other tags, including <p> and <a>, are removed from the input string.

It's important to note that while strip_tags() can help mitigate XSS attacks by removing HTML and PHP tags, it's not foolproof and should not be relied upon as the sole method of sanitizing user input. Other techniques such as input validation, output encoding, and context-specific sanitization should also be used to ensure the security of your PHP applications.

 

AHSEC| CLASS 11| GEOGRAPHY| SOLVED PAPER - 2015| H.S.1ST YEAR

  AHSEC| CLASS 11| GEOGRAPHY| SOLVED PAPER - 2015| H.S.1ST YEAR 2015 GEOGRAPHY SOLVED PAPER Full Marks: 70 Time: 3 hours The figures in the...