删除折线 [英] Removing Break Lines

查看:90
本文介绍了删除折线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过这个问题,但似乎没有得到正确的答案.我在换行时遇到了问题. Javascript和jQuery不喜欢这样的事情:

I've asked this question before but I didn't seem to get the right answer. I've got a problem with new lines in text. Javascript and jQuery don't like things like this:

alert('text

text);

当我从其中包含换行符的数据库表中提取信息时,JS和jQuery无法正确解析它.我被告知要使用n2lbr(),但是当某人在消息中键入文本时使用"shift + enter"或"enter"(这就是我遇到的问题)时,该方法不起作用.使用它时,我仍然以单独的行结尾.似乎在换行符之后正确地应用了BR标签,但仍然保留了换行符.

When I pull information from a database table that has a break line in it, JS and jQuery can't parse it correctly. I've been told to use n2lbr(), but that doesn't work when someone uses 'shift+enter' or 'enter' when typing text into a message (which is where I get this problem). I still end up with separate lines when using it. It seems to correctly apply the BR tag after the line break, but it still leaves the break there.

有人可以在这里提供一些帮助吗?我使用jQuery获取消息数据,然后将其发送到PHP文件进行存储,因此我想在此解决问题.

Can anyone provide some help here? I get the message data with jQuery and send it off to PHP file to storage, so I'd like to fix the problem there.

通常这不是问题,但是我想在用户首先加载其收件箱时拉出所有用户消息,然后在他们选择特定消息时通过jQuery将其显示给他们.

This wouldn't be a problem normally, but I want to pull all of a users messages when they first load up their inbox and then display it to them via jQuery when they select a certain message.

推荐答案

您可以使用正则表达式将换行符替换为空格:

You could use a regexp to replace newlines with spaces:

alert('<?php preg_replace("/[\n\r\f]+/m","<br />", $text); ?>');

m修饰符将跨换行符匹配,在这种情况下,我认为这很重要.

The m modifier will match across newlines, which in this case I think is important.

edit:抱歉,没有意识到您实际上想要的是<br />元素,而不是空格.相应地更新了答案.

edit: sorry, didn't realise you actually wanted <br /> elements, not spaces. updated answer accordingly.

edit2:就像@LainIwakura一样,我在正则表达式中犯了一个错误,部分原因是先前的编辑.我的新正则表达式仅替换CR/NL/LF字符,而不替换任何空格字符(\s).请注意,有一堆我没有认可的 unicode换行符字符...如果您需要处理这些问题,则可能需要阅读 regexp语法Unicode

edit2: like @LainIwakura, I made a mistake in my regexp, partly due to the previous edit. my new regexp only replaces CR/NL/LF characters, not any whitespace character (\s). note there are a bunch of unicode linebreak characters that i haven't acknowledged... if you need to deal with these, you might want to read up on the regexp syntax for unicode

这篇关于删除折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆