I want to prevent users from posting things like "aaaadfgdfgsdfgdaaaadsfgsdfgaaasdfgdfaaadfgsdfgaaaaaaaajahkjhkahkkjkjhaa..." which would not only be annoying but would certainly break the content div when the word is longer than the div itself.

I found a neat regex which solved the problem:

$thecontent = preg_replace('/\w{40}(?=\w)/', "$0-", get_the_content());

This would place hyphens inside any word having more than 40 characters in order to force a line-break if necessary and keep the layout unharmed.

The thing is I would like to apply this only when the word is plain text but certainly not when it's a url (or an email address perhaps).

Ultimately, a url like "http://www.aaaa.com/sdfsdfdfg/sdfsdsdfs/dsdsdfs.php?var1=dfsdfsdfvar2=sdfsdf&var3=dsfsfds" should remain as it is as I believe it's automatically taken care of by wordpress (I tried and created a long url and it displayed correctly in two lines so it seems to be ok).

With the regex here above, even the url is processed which is a problem.

How can I avoid to apply that regex to (email addresses and) standard transfer protocol urls such as http, https, ftp, ftps?

PS: I tried some client-side hyphenator plugins but they just don't fit my needs.

Thank you for your help.

  • 2
    what about just using css word-break: break-all;?
    – Milo
    Nov 17, 2012 at 4:24

1 Answer 1


Thank you Milo,

"word-break" alone wasn't enough as it's css3 only and not cross-browser.

But I found this:

   -ms-word-break: break-all;
    word-break: break-all;
    word-break: break-word;
   -webkit-hyphens: auto;
   -moz-hyphens: auto;
    hyphens: auto;

It seems to to work in all cases I tested. Hopefully it would be cross-browser enough to solve the problem. Not sure if it would resist to old browsers though...

Your Answer

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

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