473,706 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add character to every line of text?

384 Contributor
I have a function that takes a piece of text from the database and puts it into a textarea, I'm trying to put a symbol like '>' beside very line of text thats taken from the database and put into the textarea to show its taken from the database, can anyone help?
Jan 26 '09 #1
14 5717
Dormilich
8,658 Recognized Expert Moderator Expert
if you have defined the number of rows, then you can add before every (rows – 2) characters your indicator "> ".
Jan 26 '09 #2
ziycon
384 Contributor
The problem is, its a textarea and when the first line is populated it goes to the next line so i don't know when it will go to the next line!?
Jan 26 '09 #3
Atli
5,058 Recognized Expert Expert
Hi.

First of all, you can not put the symbol ">" into your HTML output without causing all sorts of problems. It is a reserved character.
Use the HTML symbol ">" instead. (Without the quotes.)

I'm not quite getting what you are trying to accomplish here.
Do you want to put ">" before every line of text inside your textbox?
If so, try adding the HTML symbol after each new-line character.
(Replacing all occurences of "\n" with "\n>" should do that. Again, without the quotes)

If not, please elaborate.
Remember, we like code examples and error messages ;]
Jan 26 '09 #4
ziycon
384 Contributor
I understand about the >. Ok I'll try and explain it better. I have a textarea on a page, when the page loads it get a a string, it can be a small or large string, the string is put into the textarea like below:
Expand|Select|Wrap|Line Numbers
  1. echo '<textarea name="body" cols="75" rows="10">'.getText().'</textarea>';
Now, the text is wrapped automatically to fit into the textarea so ther is no new-line characters that I'm aware of.

The textare is a message reply box so the text being taken from the database is the received message, i need to add '>' to show its the received text so when a user enters new text you can tell the difference.

Hope this makes it a bit easier too understand.
Jan 26 '09 #5
Atli
5,058 Recognized Expert Expert
Ok, so you want the old message, the one from the database, to be sent along with the new text?

Somewhat like:
> This is some text from the database.
> A text that should appear quoted.
> Which means each line should start with
> a ">" symbol.

This is a new message.
Something the current use would type
into the textarea.
If so, then you would just have to add the &gt; symbol on ever new-line.
If there are no new-lines (if the text is one long line), you could split it into lines using the wordwrap function, and have the function add the &gt; symbol on every new line.
Jan 26 '09 #6
ziycon
384 Contributor
Heres the function i was trying to use but with no luck:
Everytime data is entered into the database it adds a <p></p> to every line, if you get me?
Expand|Select|Wrap|Line Numbers
  1. function formatReplyText($msg) {
  2.     $msg = str_replace("<p>&gt;&gt;","&gt;&gt;&gt;",$msg);
  3.     $msg = str_replace("<p>&gt;","&gt;&gt;",$msg);
  4.     $msg = str_replace("<p>","&gt;",$msg);
  5.     $msg = str_replace("</p>","<br /><p>",$msg);
  6.  
  7.     //$msg = wordwrap($msg, 12, "<br />\n");
  8.     //$msg = str_replace("&gt;&gt;","&gt;&gt;&gt;",$msg);
  9.     //$msg = str_replace("&gt;","&gt;&gt;",$msg);
  10.     //$msg = str_replace("<p>","&gt;",$msg);
  11.  
  12.  
  13.  
  14.     //$msg = str_replace("</p>","<br />\n",$msg);
  15.  
  16.     return $msg;
  17. }
Jan 27 '09 #7
Atli
5,058 Recognized Expert Expert
Is the return value of that supposed to go inside the <textarea>?

If so, then you should not be using HTML tags in it.
The value of a textarea should be formatted like normal text, not HTML.

So, if your message were something like:
Expand|Select|Wrap|Line Numbers
  1. First line
  2. Second line
  3. Third line
And you did something like this to it:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function formatText($text) {
  3.   $text = wordwrap($text, 50);
  4.   $text = htmlentities($text, ENT_NOQUOTES, "UTF-8");
  5.   $text = "&gt;" . str_replace("\n", "\n&gt;", $text);
  6.  
  7.   return $text;
  8. }?>
It would display the text in lines, each line no longer than 50 characters and each line prefixed with a > symbol.

If each line is also encapsulated in <p>..</p> tags, you might want to simply remove them by using the str_replace function, passing each of the tags with an empty string.

@ziycon
This is a very very bad thing to do. You should never format your data before putting it into the database.
What if you ever decide to change the format? You would have to update you entire database to fit your new format, which puts it at risk of corruption and all sorts of other problems.

The database should always store the original text. Whatever alterations you need to make before it is displayed on your front-end application should be done on the way out.
Jan 27 '09 #8
ziycon
384 Contributor
Its showing up like so when i got to reply:
>

Test message.
I'm using TinyMCE as the editor in the textarea and TinyMCE adds <p></p> on every line.
Jan 27 '09 #9
Atli
5,058 Recognized Expert Expert
If your using the function I just posted, that would mean you are passing it an empty string. (Just made that mistake myself just now... misspelled the variable :P)

This:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function formatText($text) {
  3.   $text = wordwrap($text, 70);
  4.   $text = htmlentities($text, ENT_NOQUOTES, "UTF-8");
  5.   $text = "&gt; " . str_replace("\n", "\n&gt; ", $text);
  6.  
  7.   return $text;
  8. }
  9.  
  10. $message = <<<TEXT
  11. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed elementum. Pellentesque mattis. Vestibulum nisl tortor, blandit ut, tincidunt at, fermentum at, massa. Praesent dictum semper justo. Fusce et massa. Sed nisi tellus, venenatis vel, condimentum nec, iaculis sollicitudin, turpis. Ut sit amet urna eget eros ullamcorper ullamcorper. Integer volutpat. 
  12. TEXT;
  13. ?>
  14. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  15. <html>
  16.     <head>
  17.         <title>Test</title>
  18.     </head>
  19.     <body>
  20.         <textarea cols="72" rows="10"><?php echo formatText($message); ?></textarea>
  21.     </body>
  22. </html>
Gives:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3.     <head>
  4.         <title>Test</title>
  5.     </head>
  6.     <body>
  7.         <textarea cols="72" rows="10">&gt; Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed
  8. &gt; elementum. Pellentesque mattis. Vestibulum nisl tortor, blandit ut,
  9. &gt; tincidunt at, fermentum at, massa. Praesent dictum semper justo. Fusce
  10. &gt; et massa. Sed nisi tellus, venenatis vel, condimentum nec, iaculis
  11. &gt; sollicitudin, turpis. Ut sit amet urna eget eros ullamcorper
  12. &gt; ullamcorper. Integer volutpat. </textarea>
  13.     </body>
  14. </html>
Jan 27 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2218
by: Curious Angel | last post by:
Help? Spec Character Problems w/JAVASCRIPT TOOLTIP Hi, I'm experiencing bizarre problems with quote marks that previously displayed properly in a Javascript TOOLTIP I wrote a year ago . . . and now, inexplicably, won't translate (?). The COPYRIGHT text was originally written using Typographers Quotes (in the ALT-0145, ALT-0146, ALT-0147, and ALT-0148 family). For those who don't know what I'm talking about, these are the more stylized...
4
10962
by: Surya Kiran | last post by:
Hi all, I'm facing a wierd problem. I've a file, which is getting updated every now and then. and i'm having another program, which monitors the file. I've to read the file line by line, and in the end, i've to find out whether the line has end of line character ('\n') in it or not. if its not there (that means the line is only partially written), i've to discard it. I'm using std::string::getline() function, with the delimiter as '\n'....
3
1740
by: danmc91 | last post by:
Hi, I'm just getting going with xml and xslt. I'm trying to write what are essentially man pages and I need 3 output formats. 1) nroff -man format for real man pages 2) html for an online help browser 3) ascii where every line must start with a '%' character (octave
2
3967
by: Mike Turco | last post by:
I have a bunch of text files I'm trying to parse. The files all have several occurrences of chr(26), which is EOF (End Of File). Each file is ~ 1meg of text, and every file is a real mess. The looks something like this: Date=1/1/04-=time4:20pmSalesperson-=-)JIM HENDERSON$%L_#*(%-+* da_FIRST.........------MIKE da_LAST .........------TURCO da_PRODUCT.....------BOAT da_YY...........------04 )(#$%^&*
7
96312
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
18
4622
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also has some different characters in it that I am trying to remove. The small a with two dots over it and the small y with two dots over it. Here is my code so far to remove the small y: Private Sub Button2_Click(ByVal sender As System.Object, ByVal...
1
2454
by: Kenneth McDonald | last post by:
I am going to demonstrate my complete lack of understanding as to going back and forth between character encodings, so I hope someone out there can shed some light on this. I have always depended on the kindness of strangers... :-) I'm playing around with some very simplistic french to english translation. As some text to work with, I copied the following from a french news site:
2
1976
by: Nathan Sokalski | last post by:
I have a page which reads the the first line of every *.txt file in a certain directory of mine to use as the Text property of a HyperLink Control. However, in one of my recently added text files in the directory it is using all but the ellipsis character (this is the last character in the line, but it does the same thing when I place the character in other places). For example, here is what the line looks like in the *.txt file and the...
4
7391
by: Andyza | last post by:
I'm using FileSystemObject to open and write to a tab delimited text file. First, I connect to a database and select some data. Then I create the text file and insert each record in the text file. At the end of each record I insert a vbCrLf character in the text file to create a new line. Thus each record is in a new line. The problem is that I get a tab delimited text file where the LAST LINE in the text file is always a blank line....
7
13911
by: stef | last post by:
hello, In the previous language I used, when reading a line by readline, the EOL character was removed. Now I'm reading a text-file with CR+LF at the end of each line, Datafile = open(filename,'r') line = Datafile.readline() now this gives an extra empty line
0
8697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9286
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9156
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7911
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5939
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2502
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.