I'm looking to use a regex to replace sequential runs of non-whitespace characters (say more than 35) with only the first 35 characters. I would like to allow strings with "http" in them to remain as they are (so as not to break links).
The strings will be from user input, and if somebody types 50 'x' characters in a row it may go outside of my <DIV>
container and disrupt the layout. The runs might come at the beginning of a line or in the middle of one.
E.G. I would like to disallow these types of input:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
12345 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
but not these:
http://somesite.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
12345 http://somesite.com/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I got the idea of using a negative lookaround from this question
I'm getting mixed results w/ this regex:
$comment=preg_replace('/^(((?!http).){25})(((?!http).)*)$/imUs', '$1',$comment);
That regex is preserving links, but it is also trimming acceptable text down to 25 characters.
text text text text text text text text text text text text text text text text text text text text text text text text
is becoming
text text text text tex
From reading regex's from other questions, I have a feeling that this can be done with a more elegant regex than I show above. Thanks for any suggestions.