The strip_tags(), and stripslashes() functions are generally known and used in PHP. These functions are useful when working with strings that contains HTML code, but JavaScript doesn't have such functions.
Here's these functions for JavaScript.

strip_tags

strip_tags(string, allow) - returns a string with HTML and PHP tags stripped from a given string. HTML comments are also stripped.
The allow parameter is optional, and represents a string with allowable tags, which should not be stripped (a string like: '<b><i><u>').
Code:
function strip_tags(str, allow){
 // making sure the allow arg is a string containing only tags in lowercase (<a><b><c>)
 allow = (((allow || '') + '').toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');

 var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi;
 var commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
 return str.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
 return allow.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 :'';
 });
}

Example:
<div id='sresult'>#sresult</div>

<script>
function strip_tags(str, allow){
 // making sure the allow arg is a string containing only tags in lowercase (<a><b><c>)
 allow = (((allow || '') + '').toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');

 var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi;
 var commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
 return str.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
 return allow.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 :'';
 });
}

var str = '<p>Test paragraph.</p><!-- Comment --> <a href="https://coursesweb.net/">CoursesWeb.net</a>.<br>New line: <b>Bold</b>, and <u>Underline</u>. An opening Span tag: <span>*';

var str1 = strip_tags(str); // removes all tags
var str2 = strip_tags(str, '<b><u>'); // keeps <b> and <u> tags

// adds str1 and str2 in webpage
document.getElementById('sresult').innerHTML = 'str1: '+ str1+ '<br/>str2: '+ str2;
</script>
Result:
str1: Test paragraph. CoursesWeb.net.New line: Bold, and Underline. An opening Span tag: *
str2: Test paragraph. CoursesWeb.net.New line: Bold, and Underline. An opening Span tag: *

stripslashes

stripslashes() - un-quotes a quoted string. Returns a string with backslashes stripped off. (\', and \' become ' and ') Double backslashes (\\) are made into a single backslash (\).
Code:
function stripslashes(str) {
 return str.replace(/\\'/g,'\'').replace(/\"/g,'"').replace(/\\\\/g,'\\').replace(/\\0/g,'\0');
}

Example:
// stripslashes
function stripslashes(str) {
 return str.replace(/\\'/g,'\'').replace(/\"/g,'"').replace(/\\\\/g,'\\').replace(/\\0/g,'\0');
}

var str = 'String with \"backslashes\", \\ coursesweb.net/';
str = stripslashes(str);
str = stripslashes(str);

document.write(str); // String with 'backslashes', \ coursesweb.net/

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
JavaScript strip_tags and stripslashes

Last accessed pages

  1. Convert XML to JSON in JavaScript (18610)
  2. SHA512 Encrypt hash in JavaScript (10957)
  3. Node.js Move and Copy Directory (1920)
  4. Disable button and Enable it after specified time (6738)
  5. Insert, Select and Update NULL value in MySQL (30220)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (516)
  2. Read Excel file data in PHP - PhpExcelReader (430)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (319)
  4. PHP getElementById and getElementsByTagName (255)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (234)