2

我在 PHP 中使用了 strip_tags,在它处理完字符串之后,字符串现在也不包含 \n 了。

这个标准有strip_tags吗?

4

2 回答 2

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);
    }
}

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

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

于 2010-10-29T13:59:43.733 回答
3

Strip_tags 不应该删除 \n 但也许它会删除<br>.

尝试添加标签列表以允许:

strip_tags('Hello<br>World', '<br>');

这允许<br>标签留在字符串中。

于 2010-10-29T13:58:12.910 回答