如何过滤此行“XX\r\n”到XX?
问题就在标题里。我尝试了 strip_tags,但显示为空白,我尝试使用 preg_replace,但我显然不太理解语法。
尝试过这个
$test = "Home<i>\r</i><i>\n</i>";
$patterns = array();
$patterns[0] = '<i>';
$test = preg_replace($patterns, '', $test);
,但 echo $test 仍然相同!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想获取字符串的第一部分直到第一个标签,那么如何:
如果您想删除所有标签,则 strip_tags 应该这样做,或者您可以使用 preg_replace ,例如:
if you want to just get the first part of the string up to the first tag, how about something like:
if you want to remove all tags, strip_tags should do it, or you can use preg_replace like:
strip_tags
将返回 XX\r\ n,所以preg_replace
可能是你正在寻找什么,假设它实际上可以做你想做的事。我自己不知道它是否会像您想要的那样删除标签,但如果您只是不理解语法,请阅读 此处。希望这有帮助。
strip_tags
would return XX\r\n, sopreg_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.
preg_replace() 的语法问题在于您没有将模式定义为准确的正则表达式。如果您使用 str_replace(),您的方法将有效,但对于 preg_replace(),识别
的正确模式是
'//'
。基本上,您需要将其括在/
字符中以将其定义为正则表达式。如果您希望删除所有标签,Jonathan 的表达式比逐个模式实现要好得多。如果没有,您可以像这样处理特定标签:
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: