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

Rebecca
  • 13,914
  • 10
  • 95
  • 136
Madhu
  • 147
  • 1
  • 2
  • 11
  • 4
    +1 for overrunning the SO layout. Obviously they didn't use any of the answers here. – John K Jan 19 '10 at 05:59
  • Thanks for your reply. I found another way to achieve this without using overflow: hidden property. Simply by making use of
    Thanks once again guys.
    – Madhu Jan 19 '10 at 06:12
  • I had included `break-work` as an edit to my answer too. – John K Jan 19 '10 at 06: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 '10 at 06:28

7 Answers7

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>
John K
  • 28,441
  • 31
  • 139
  • 229
  • I agree... this is definitely the most reliable way to go. – Yevgeny Simkin Jan 19 '10 at 05: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 '10 at 05: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 '10 at 05:55
1

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

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
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;)

Community
  • 1
  • 1
Ryann Graham
  • 8,079
  • 2
  • 29
  • 32
  • 1
    As far as I know this isn't cross browser: http://gojomo.blogspot.com/2005/03/cross-browser-invisible-word-break-in.html – vdh_ant Jan 19 '10 at 05:26
  • That's the impression I got from the linked SO question. Probably should've opened with that. – Ryann Graham Jan 19 '10 at 05:45
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);
anthonyv
  • 2,455
  • 1
  • 14
  • 18
0

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

YOURTEXT=YOURTEXT.replace(/(.)/g,"\u200b$1");
YOU
  • 120,166
  • 34
  • 186
  • 219
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.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
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>
Tarnay Kálmán
  • 6,907
  • 5
  • 46
  • 57