PHP strip标签也删除\n

DRK*_*RKM 2 php strip-tags

我在PHP中使用strip_tags并且在处理完字符串之后,字符串现在也不包含\n的..

这个标准是strip_tags吗?

Ern*_*est 9

嗯,难以测试吗?:)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这个测试将通过.使用单引号时的结果相同.所以,不,strip_tags不会删除\n.

编辑:正如其他人在这里指出的那样 - strip_tags可能会删除<br>你的标签.此外,下次,如果您提供一些代码,您将更快地得到答案.添加了两个新测试:)

  • 用单元测试来回答 - 这很不错+1 (5认同)