PHP 剥离标签也删除 \n

发布于 2024-09-29 23:09:10 字数 79 浏览 0 评论 0原文

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

这是 strip_tags 的标准吗?

I'm using strip_tags in PHP and after it has processed the string, the string now also contains no \n's..

Is this standard with strip_tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…红帽 2024-10-06 23:09:10

嗯,测试有那么难吗? :)

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 可能会删除您案例中的
标签。另外,下次如果您提供一些代码,您会更快地得到答案。
添加了两个新测试:)

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

This test will pass. The same result is while using single quotes. So, no, strip_tags doesn't remove \n.

EDIT:
Just as already other people here pointed out - strip_tags probably removes <br> tag in your case. Also, next time, if you provide some code, you will get your answer quicker.
Added two new tests :)

云朵有点甜 2024-10-06 23:09:10

Strip_tags 不应删除 \n,但可能会删除

尝试添加要允许的标签列表:

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

这应该允许
标签保留在字符串中。

Strip_tags should not remove \n but maybe it removes <br>.

Try adding a list of tags to permit:

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

this shold allow <br> tags to stay in the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文