1

Please dont mind for adding a vulnarable content as below.

jffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj

I have a multiline html text editor(tiny mce). When a user enters unappropriate words, as i entered above!. It will be saved properly in database. When i am trying to display the same data using a label. The displayed data disturbs the page design.

How can i fix this issue? Please provide me a solution for this.

Thanks in advance Regards, Madhu BN

4
  • 4
    +1 for overrunning the SO layout. Obviously they didn't use any of the answers here.
    – John K
    Jan 19, 2010 at 5:59
  • Thanks for your reply. I found another way to achieve this without using overflow: hidden property. Simply by making use of <div style="word-wrap: break-word; width: 500px;"> Thanks once again guys.
    – Madhu
    Jan 19, 2010 at 6:12
  • I had included break-work as an edit to my answer too.
    – John K
    Jan 19, 2010 at 6:14
  • Sorry i was not yet refreshed the page. Yes, you are perfect. I am trying to put the width as 100%. At that time word wrap is going out of my parent div whose width is 100% too.
    – Madhu
    Jan 19, 2010 at 6:28

7 Answers 7

5

If it's about disturbing the page design when redisplaying the user's input and if the input is "inappropriate" then apply a CSS style to cut it off by using overflow:hidden.

<style> 
    .fixed { overflow:hidden; }
</style>

Then apply it to the div or container:

<div class="fixed" style="width:100px">The following input is really long and invalid. 

fffffffffffffffffffffffffffffffffffffffffffffffffffffjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj

jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj</div>

This ensures the page layout is not disturbed. In the above example 100px is adhered to no matter the unbreaking length of the invalid text.

Edits:

If you want the wrapping behaviour try using CSS word-wrap: break-word;

<style>
.fixed {
    word-wrap: break-word;
}
</style>

Or even put them both together to be really safe across browsers.

<style> 
    .fixed { 
        overflow:hidden; 
        word-wrap: break-word;
    }
</style>
3
  • I agree... this is definitely the most reliable way to go. Jan 19, 2010 at 5:31
  • Thanks a lot sir. Without hiding the text, cant we break the content to move to next line. It works fine if i am making use of width in terms of pixel. What can i do if i am using %.
    – Madhu
    Jan 19, 2010 at 5:46
  • I made an edit to show another option. Styles should exhibit the same behaviour whether you're using a pixel width like width:100px or a percentage like width:90%
    – John K
    Jan 19, 2010 at 5:55
1

If you use PHP to print out the text, check out the wordwrap function.

1
  • 1
    Sorry for not mentioning the programming technology. I am using c# with asp.net
    – Madhu
    Jan 19, 2010 at 5:17
1

Try inserting &shy; into the string. It's the HTML element for the "soft hyphen".

If using PHP, print(wordwrap($string, 75, '&shy;'));

More info on SO: Soft hyphen in HTML (<wbr> vs. &shy;)

2
1

This is a crossbrowser solution that I was looking at a little while ago that runs on the client and using jQuery:

(function($) {
  $.fn.breakWords = function() {
    this.each(function() {
      if(this.nodeType !== 1) { return; }

      if(this.currentStyle && typeof this.currentStyle.wordBreak === 'string') {
        //Lazy Function Definition Pattern, Peter's Blog
        //From http://peter.michaux.ca/article/3556
        this.runtimeStyle.wordBreak = 'break-all';
      }
      else if(document.createTreeWalker) {

        //Faster Trim in Javascript, Flagrant Badassery
        //http://blog.stevenlevithan.com/archives/faster-trim-javascript

        var trim = function(str) {
          str = str.replace(/^\s\s*/, '');
          var ws = /\s/,
          i = str.length;
          while (ws.test(str.charAt(--i)));
          return str.slice(0, i + 1);
        };

        //Lazy Function Definition Pattern, Peter's Blog
        //From http://peter.michaux.ca/article/3556

        //For Opera, Safari, and Firefox
        var dWalker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT, null, false);
        var node,s,c = String.fromCharCode('8203');
        while (dWalker.nextNode()) {
          node = dWalker.currentNode;
          //we need to trim String otherwise Firefox will display
          //incorect text-indent with space characters
          s = trim( node.nodeValue ).split('').join(c);
          node.nodeValue = s;
        }
      }
    });

    return this;
  };
})(jQuery);
0

In javascript, add \u200b - Zero Width Space before displaying it

YOURTEXT=YOURTEXT.replace(/(.)/g,"\u200b$1");
0

You could use InsertAt repeatedly to achieve @Ryan's solution, or you could perform validation to prevent such malformed data from reaching the database.

Regular expressions would also make this available to put the soft-hyphen in.

0

It can be fixed with a little CSS using overflow-x (lots of cool examples after link).

Just make sure you specify a width, otherwise overflow-x won't do you any good.

Try it here

<style>
div{
  width: 105px;
  overflow-x: hidden;
  float: left;
  margin: 10px;
  padding: 4px;
  background: orange;
}
</style>
<div>WAYTOOLONGTOBESHOWN</div>
<div>WAYTOOLONGTOBESHOWN</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.