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);
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.
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.
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); ?>');
The m modifier will match across newlines, which in this case I think is important.
edit: sorry, didn't realise you actually wanted <br /> elements, not spaces. updated answer accordingly.
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
Edit: Okay after much tripping over myself I believe you want this:
$str = preg_replace('/\n+/', '<br />', $str);
And with that I'm going to bed...too late to be answering questions.
I usually use json_encode() to format string for use in JavaScript, as it does everything that's necessary for making JS-valid value.
Related
I spent hours looking at dozens of different solutions but none of them worked. I am receiving the content of a textarea in a php string and I would like to remove anything which is more than one blank line.
Example ok
Hi how are you,
// one blank line here so ok to keep
Not too bad thanks
Example not ok
hi how are you
// two lines (or more) here so we remove one and keep the other
not too bad thanks
Does someone know the proper preg_replace to use? Note that I don't want to modify the data (no nl2br() please) as it is easier for me to keep it raw (ios support).
Maybe you should try this:
preg_replace('/\n\r(\n\r)+/', "\n\r", $str);
You could try
preg_replace("/[\r\n]+/", "\n", $text);
It replaces one (or more) newline or carriage return with a single newline.
I have a system set up for users to submit their articles into my database. Since it will just be HTML, I don't want to expect them to know to type <br /> every time there's a newline, so I am using the PHP function nl2br() on the input.
I'm also providing an article modification tool, which will bring their articles back into the form (this is a different page, however) and allow them to edit it. In doing this, the <br /> elements were appearing also (with newlines still). To remedy the elements appearing (which I had expected, anyway) I added preg_replace('/<br(\s+)?\/?>/i', "\n", mysql_result($result,$i,"content")) which I had found in another question on this site. It does the job of removing the <br /> elements, but since it is replacing them with newlines, and the newlines would have remained originally anyway, every time the post is edited, more and more newlines will be added, spacing out the paragraphs more and more each time. This is something a user won't understand.
As an example, say I enter the following into the article submission form:
Hello, this is my article.
I am demonstrating a new line here.
This will convert to:
Hello, this is my article.<br />
I am demonstrating a new line here.
Notice that, even though the newline character was converted, there is still a newline in the text. In the editing form, the <br /> will be converted back to newline and look like this:
Hello, this is my article.
I am demonstrating a new line here.
Because the <br /> was converted to a newline, but there was already a newline. So I guess what I'm expecting is for it to originally be converted to something like this:
Hello, this is my article.<br />I am demonstrating a new line here.
I'm wondering ... is there a way to stop the nl2br() function from maintaining the original newlines? Might it have to do with the Windows \r\n character?
The function you're using, nl2br is used for inserting them, but not replacing them. If you want to replace \n with <br /> you just need to use str_replace. Like so:
$string = str_replace("\n","<br />",$string);
There is absolutely no need for regex in this situation.
It seems like the problem you described is not a bug, but a feature of bl2br. You could just write your own function for it, like:
<?php
function NlToBr($inString)
{
return preg_replace("%\n%", "<br>", $inString);
}
?>
I found this one in the comments of the documentation of the nl2br-function in the PHP Manual: http://php.net/manual/de/function.nl2br.php. If the one I posted did not work for you, there should be plenty more where it came from.
(Or just use the function from the other Answer that was just posted, I guess that should work, too)
This should fix it:
preg_replace('/<br(\s+)?\/?>(?!\s*\n)/i', "\n", mysql_result($result,$i,"content"))
You cannot simply remove the breaks, because they might be on the same line. This regex will replace all breaks with newline but not those that are followed by the newline.
It will leave the <br>\n in the text. Additional regex will get rid of them:
preg_replace('/<br(\s+)?\/?>/i', "", $res)
I need some advice ...
I'd like to know if it is a good practice to use in a code line breaks "\n"? What is the purpose?
$my_string .= "\n" . "<p>Some values</p>" . "\n";
Till now I haven't use it and I'd really like to know ... your opinion.
Thanks
You only need to use "\n" to make the resulting html code neater/easier to read. It is not necessary.
The new line makes your generated code easier to read. You might think noone should read your code, however if you run into some problems the generated code is easier to read for debugging purposes as well.
\n can be used when streaming over sockets as well. Sometimes you need to use \r, depending on the operating system.
This is very common mistake of the newbie programmers.
They never have an idea that the result of the PHP script execution is plain HTML code.
And sometimes a programmer have to sort things out with that HTML.
While it's just impossible if there are no linegreaks in the code.
Anyway, a good practice would be
$my_string = "Some values";
?>
<p><?=$my_string</p>
so, you won't need no special linebreaks in the PHP code.
Also, there are some cases where you have to use linebreaks.
For example, if you are composing an HTML email message, you hve to add linebreaks, or they will be forcibly added in the unexpected places.
at time of writing to break line, means display text after line-break to next line
like enter in some text editor...
The only purpose is for people using the "View Source" feature of the browser - basically no one. While it is considered good practice, I almost never do (because it's pointless) :)
for pre-tag (pre formatted text):- http://webdesign.about.com/od/htmltags/f/blfaqpre.htm
so, you can do this in SO
1
2
3
4
5
6
7
8
\n is just an escape for a new line. An escape is not really required in PHP, but it makes it seamless to output a new line mark.
Essentially I have this
<p>hello</p>
<p>So I wanted just to say hi</p>
<p>I hope its going well</p>
Coming from a db.
If I just strip the tags then in the title I get this
title="helloSo I wanted just to say hiI hope its going well"
What I want (like SO does it).
title="Hello
So I wanted just to say hi
I hope its going well"
I have tried using \n or \r and it just shows up in the title text.
title="Hello\n\rSo I wanted just to say hi\n\rI hope its going well"
Is this possible because of using Markdown and the way SO is saving the text?
I'm using TinyMCE and I have looked into ways of formatting the text, I've even tried using the output buffer to try and arrange the text how I want it.
Edit: Lets make this really clear and simple. It's going into a TITLE element!
If I can't get this to work, I'll just do this via a popup with jQuery.
Any help / advise appreciated :)
In PHP, The string '\r\n' is interpreted just like that and consist of four letters. The string "\r\n" is interpreted as escape sequences and contains two characters, a carriage return and a linefeed. Use double quotes if you want to insert a line break in a string.
If I recall correctly, browsers won't render alt/title tags with multiple lines? It will simply remove the line spacing.
If they do now, then PHP requires you to wrap \r\n\t and other formatting in " double quotations as Sjoerd suggested.
I have a text area in HTML where the user can enter text, but when the form is submitted, and its passed to a php script which echo's it, there aren't any newlines. Knowing that HTML does that, I tried doing a preg_replace() before echoing it...
echo preg_replace("/\n/", "<br />", $_GET["text"]);
but still everything is on one line.
So my best guess is that HTML textareas use a different newline character... can anybody shed some light on the subject?
EDIT
Ok, so I've figured out the problem: The Javascript is stripping the newlines. view code here
EDIT 2
Ok, so thanks to Jason for solving this problem. I needed to do:
escape(document.getElementById('text'));
Instead of just:
document.getElementById('text');
and the newlines are preserved, problem solved!
echo nl2br($_GET['text'])
Though, your preg_replace worked for me!
usually when testing for newlines in any string, I use /[\n\r]/, just to cover my bases. My guess is that this would match the new lines.