如何过滤此行“XX\r\n”到XX?

发布于 2024-09-09 04:40:24 字数 298 浏览 3 评论 0 原文

问题就在标题里。我尝试了 strip_tags,但显示为空白,我尝试使用 preg_replace,但我显然不太理解语法。

尝试过这个

    $test = "Home<i>\r</i><i>\n</i>";       
$patterns = array();
$patterns[0] = '<i>';
$test = preg_replace($patterns, '', $test);

,但 echo $test 仍然相同!

Question is in the title. I tried strip_tags, but that shows up blank and I tried using preg_replace, but I don't quite understand the syntax apparently..

Tried this

    $test = "Home<i>\r</i><i>\n</i>";       
$patterns = array();
$patterns[0] = '<i>';
$test = preg_replace($patterns, '', $test);

but echo $test is still the same!

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

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

发布评论

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

评论(3

空心↖ 2024-09-16 04:40:24

如果您只想获取字符串的第一部分直到第一个标签,那么如何:

$test = "Home<i>\r</i><i>\n</i>";
preg_match('/^[^<]+/', $test, $match);
echo $match[0];

如果您想删除所有标签,则 strip_tags 应该这样做,或者您可以使用 preg_replace ,例如:

$test = "Home<i>\r</i><i>\n</i>";
$test = preg_replace('/<[^>]+?>/', '', $test);
echo $test;

if you want to just get the first part of the string up to the first tag, how about something like:

$test = "Home<i>\r</i><i>\n</i>";
preg_match('/^[^<]+/', $test, $match);
echo $match[0];

if you want to remove all tags, strip_tags should do it, or you can use preg_replace like:

$test = "Home<i>\r</i><i>\n</i>";
$test = preg_replace('/<[^>]+?>/', '', $test);
echo $test;
戒ㄋ 2024-09-16 04:40:24

strip_tags 将返回 XX\r\ n,所以 preg_replace 可能是你正在寻找什么,假设它实际上可以做你想做的事。我自己不知道它是否会像您想要的那样删除标签,但如果您只是不理解语法,请阅读 此处

希望这有帮助。

strip_tags would return XX\r\n, so preg_replace would probably be what you're looking for, assuming it can actually do what you want to do. I don't know myself whether or not it will strip the tags like you want, but if you just don't understand the syntax, read up here.

Hope this helps.

离不开的别离 2024-09-16 04:40:24

preg_replace() 的语法问题在于您没有将模式定义为准确的正则表达式。如果您使用 str_replace(),您的方法将有效,但对于 preg_replace(),识别 的正确模式是 '//'。基本上,您需要将其括在 / 字符中以将其定义为正则表达式。

如果您希望删除所有标签,Jonathan 的表达式比逐个模式实现要好得多。如果没有,您可以像这样处理特定标签:

$test = "Home<i>\r</i><i>\n</i>";       
$patterns = array();
$patterns[0] = '/<\/?i>/'; //removes <i> and </i>
$patterns[1] = '/\r|\n/'; //removes \r and \n
$test = preg_replace($patterns, '', $test);

The problem with your syntax on preg_replace() is that you did not define the pattern as an accurate regular expression. Your method would work if you used str_replace(), but for preg_replace(), the correct pattern for identifying <i> is '/<i>/'. Basically, you need to enclose it in / chars to define it as a regex.

Jonathan's expression is much better than a pattern by pattern implementation, if you are looking to remove all tags. If not, you can handle specific tags like this:

$test = "Home<i>\r</i><i>\n</i>";       
$patterns = array();
$patterns[0] = '/<\/?i>/'; //removes <i> and </i>
$patterns[1] = '/\r|\n/'; //removes \r and \n
$test = preg_replace($patterns, '', $test);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文