Jump to content

Long Strings


mortalc

Recommended Posts

What there is:-A form which writes text to the end of a fileThe Problem:The text in question will be very long, and contain many special characters. Here's an example:Bob went home."I am very tired," he said.He went to bed.The next day, Bob woke up."It's a nice day today" he said.etc.I'm sure that if I just wrote it like this, it wouldn't work, what with all the "s, 's, and new lines. Is there any way at all to 'correct' the string, or will I have to manually do it every time? I expect it to be used by users who will not know javascript, and so will not know, for example, that /n means new line.ED: A good example of how it might work is this forum. I don't have to write /n, something does it for me.

Link to comment
Share on other sites

Did you try it? If a user types that text into a form field then there should be no problems. It's only when you are typing strings literally, in the code, that you have to worry about the string literal delimiter (i.e. ") appearing inadvertently.P.S. when you are entering text and press the return key, you are actually typing the newline character - the control sequence \n (backslash-n) is just an alternative representation in some programming languages.P.P.S. you may run into trouble if the text contains actual special characters (i.e. non-ASCII ones). You'll have to check the encoding of the text file in that case.

Link to comment
Share on other sites

What will happen is that the string will be written from the form to a text file, so there will be no coding. However, this file will be shown on the webpage using a string.

Link to comment
Share on other sites

Oh - well, if you are fetching the text file directly with the XMLHttpRequest object, then you can just replace the quotation marks with their entities, and newlines with break tags, using the replace method. If you are going through PHP you can use htmlspecialchars() and nl2br().

Link to comment
Share on other sites

Sounds like you're thinking about this in a reverse way... XMLHttpRequest() sends a request to PHP and it receives something (arbitrary text; no such thing as "special characters" in this context) as response from PHP. You can do anything with that response, including outputting it.Because you want to output the response inside an HTML document as plain text, you need to make sure you either use JavaScript to write the content as plain text (possible, but not exactly trivial), or make PHP give a kind of a response that can be written out as is by JavaScript with something as trivial as innerHTML.If from PHP you have something like:

echo nl2br(htmlspecialchars(file_get_contents('test.txt')));

Where "test.txt" contains for example:

R&B bro.And they all said "Happy ever after".It's time.This < That.But that > this other thing.

Then the PHP file would output:

R&B bro.<br />And they all said "Happy ever after".<br />It's time.<br />This < That.<br />But that > this other thing.

Which is a string you can safely output as HTML plain text with JavaScript like:

element.innerHTML = xhr.responseText;

Link to comment
Share on other sites

Let me just get my head around this...

echo nl2br(htmlspecialchars(file_get_contents('test.txt')));

As far as I can tell, this will do the job. (just out of interest - what is the nl2br bit?)I didn't quite understand the last bit of code - the javascript one.
Link to comment
Share on other sites

Let me just get my head around this...As far as I can tell, this will do the job. (just out of interest - what is the nl2br bit?)I didn't quite understand the last bit of code - the javascript one.
element would be a reference to an element obtained using document.getElementById().innerHTML is a property that you can moedify to directly insert text/HTML right into the element, and it will show up on the pageresponseText is one of the return value's of the xmlHttpRequest object (if the response isn't XML, which would be responseXML)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...