PHP strip tags also removing \n

Active3 hr before
Viewed126 times

6 Answers


Meta Stack Overflow ,Thanks for contributing an answer to Stack Overflow!, Stack Overflow help chat , Stack Overflow Public questions & answers

Well, is it so hard to test? :)

class StripTagsTest extends PHPUnit_Framework_TestCase {
    public function testStripTagsShouldNotRemoveLF() {
        $input = "Hello\n <b>World</b>\n";
        $actual = strip_tags($input);
        $expected = "Hello\n World\n";
        $this->assertEquals($expected, $actual);

   public function testStripTagsRemovesBRTagByDefault() {
        $expected = "HelloWorld\n";
        $input = "Hello<br>World<br>\n";
        $actual = strip_tags($input);
        $this->assertEquals($expected, $actual);

        $input = "Hello</br>World</br>\n";
        $actual = strip_tags($input);
        $this->assertEquals($expected, $actual);

    public function testStripTagsCanPermitBRTags() {
        $expected = "Hello<br>World<br>\n";
        $actual = strip_tags($expected, '<br>');
        $this->assertEquals($expected, $actual);

        $expected = "Hello</br>World</br>\n";
        $actual = strip_tags($expected, '<br>');
        $this->assertEquals($expected, $actual);
load more v

HTML comments and PHP tags are also stripped. This is hardcoded and can not be changed with allowed_tags. , strip_​tags ,Example #1 strip_tags() example,strip_tags — Strip HTML and PHP tags from a string

Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>

Return Value: This strip_tags function gives us the resultant string stripped off from the input string.,Some of the things that are stripped by default are the HTML comments and PHP tags and these things cannot be changed as they are hard-coded.,Let us now take some examples to understand the working of PHP strip_tags function.,In the above example, we are displaying the use of recursive functions for strip_tags function. Hence in the output we can see that the array is printed in loops of 2.

string strip_tags( $str, $allowed_tags )

string strip_tags($str, $allowed_tags)
load more v

Return Value: This function returns an array if the subject parameter is an array, or a string otherwise.Example: This example use preg_replace() function to remove line break.  ,How to remove line breaks from the string in PHP?,PHP | str_replace() Function,Using preg_replace() function: The preg_replace() function is an inbuilt function in PHP which is used to perform a regular expression for search and replace the content.Syntax:  

load more v

An implementation of PHP's strip_tags in Node.js.,striptags can also operate in streaming mode. Simply call init_streaming_mode to get back a function that accepts HTML and outputs stripped HTML. State is saved between calls so that partial HTML can be safely passed in.,striptags does not use any regular expressions for stripping HTML tags.,Check out test/striptags-test.js for a concrete example.


npm install striptags
striptags(html, allowed_tags, tag_replacement);
var striptags = require('striptags');

var html =
'<a href="">' +
   'lorem ipsum <strong>dolor</strong> <em>sit</em> amet' +

striptags(html, '<strong>');
   striptags(html, ['a']);
   striptags(html, [], '\n');


'lorem ipsum dolor sit amet'
lorem ipsum <strong>dolor</strong> sit amet'
'<a href="">lorem ipsum dolor sit amet</a>'
lorem ipsum

let stream_function = striptags.init_streaming_mode(

let partial_text = stream_function(partial_html);
let more_text = stream_function(more_html);

You can run tests (powered by mocha) locally via:

npm test

Generate test coverage (powered by istanbul) via :

npm run coverage
load more v