Solved

Javascript / PHP format data field

Posted on 2010-08-31
4
216 Views
Last Modified: 2013-12-13
I have a varchar data field called notes that is displayed in javascript like:

 var text = "<div style=\"white-space:nowrap;\"><div align=\"left\" class=\"smalltext\"><strong>"+name +"</strong><BR>"+address +"<br>Phone: "+phone +em +web +"<BR><EM>"+notes +"</EM></div></div>";

Open in new window


How do I alter the +notes + to insert a <BR> after 10 words or if words are not possible, then 25 characters.
0
Comment
Question by:pda4me
  • 2
4 Comments
 
LVL 6

Expert Comment

by:apresence
ID: 33565793
Attached code will split it up into 25 character blocks.
<SCRIPT language="JavaScript">

name = 'John Doe';

address = '100 Nowheresville';

phone = '800 555-1212';

em = 'em';

web = 'http://www.fofdsjklfasjflkasjfl.com/';

notes = 'This is a list of information that should be displayed as notes on the screen for the given user';



function split_text(text, char_count)

{

  new_text = '';



  while (text.length > 0)

  {

    if (new_text.length > 0) new_text += '<br>';

    new_text += text.substr(0, char_count);

    text = text.substr(char_count);

  }



  return new_text;

}



notes = split_text(notes, 25);

var text = '<div style="white-space:nowrap;"><div align="left" class="smalltext"><strong>'+ name +'</strong><BR>'+address +'<br>Phone: '+phone +em +web +'<BR><EM>'+notes +'</EM></div></div>';



document.write(text);

</SCRIPT>

Open in new window

0
 
LVL 13

Expert Comment

by:ansudhindra
ID: 33565814
You can use chr(13) and chr(10) from your server side script to add <br> .
In coldfusion it is

Text...#chr(13)##chr(10)#... text which actually displays like

Text.....
...text

Just print these two chars wherever you want line breaks
0
 
LVL 6

Accepted Solution

by:
apresence earned 500 total points
ID: 33565832
Attached code will split up into 10-word lines.
<SCRIPT language="JavaScript">

name = 'John Doe';

address = '100 Nowheresville';

phone = '800 555-1212';

em = 'em';

web = 'http://www.fofdsjklfasjflkasjfl.com/';

notes =

  'This is a list of information that should be displayed as notes ' +

  'on the screen for the given user. This time we\'re splitting based ' +

  'on the number of words, 10 per line.';



function split_text_words(text, word_count)

{

  new_text = '';

  words = 0;



  for (i=0; i<text.length; i++)

  {

    ch = text.substr(i, 1);



    if (ch == ' ')

    {

      words++;

      if (words == word_count)

      {

        new_text += '<br>';

        words = 0;

      }

      else

      {

        new_text += ch;

      }

    }

    else

    {

      new_text += ch;

    }

  }



  return new_text;

}



notes = split_text_words(notes, 10);

var text = '<div style="white-space:nowrap;"><div align="left" class="smalltext"><strong>'+ name +'</strong><BR>'+address +'<br>Phone: '+phone +em +web +'<BR><EM>'+notes +'</EM></div></div>';



document.write(text);

</SCRIPT>

Open in new window

0
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33565836
Split by 10 words..
var newnotes = note.split(" ");

var newstr = "";

for (i=0; i<newnotes.length; i++)

{

   if(y%10==0)  

      	newstr = words[i] + "<br>"

   else

    	newstr = words[i]

}     



var text = "<div style=\"white-space:nowrap;\"><div align=\"left\" class=\"smalltext\"><strong>"+name +"</strong><BR>"+address +"<br>Phone: "+phone +em +web +"<BR><EM>"+newstr  +"</EM></div></div>";

Open in new window

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now