Geek Alert!

• Views: 1,216

I’m pretty clueless when it comes to regular expressions, although I’m finally beginning to learn how to use them effectively. Here’s my latest problem, for which any suggestions from regex experts would be greatly appreciated.

Occasionally someone will post a comment containing a very long string of unbroken characters, which causes the comments area to expand, and thoroughly wrecks the display. I’d like to have a regular expression (PHP style) that checks comments for long strings, inserts spaces as appropriate (say, on a 40-character boundary), yet leaves any long strings inside HTML tags alone (because hyperlinks can be longer without causing a problem).

Any solutions, oh mighty hordes of regex-savvy lizardoids?

UPDATE: Thanks to all for the suggestions and offers to help; reader Doug Stewart found a piece of code at php.net that solves the problem nicely. Here’s the final result, which breaks any word longer than 40 characters by inserting a space, but ignores long words inside HTML tags:

$wrap_at = 40;
$htmltext = stripslashes (preg_replace (‘%(\s*)([^>]{‘.$wrap_at.’,})(<|$)%e’, “’\1’.wordwrap(‘\2’, ‘”.$wrap_at.”’, ’ ‘, 1).’\3’”, $htmltext));

Jump to top

Create a PageThis is the LGF Pages posting bookmarklet. To use it, drag this button to your browser's bookmark bar, and title it 'LGF Pages' (or whatever you like). Then browse to a site you want to post, select some text on the page to use for a quote, click the bookmarklet, and the Pages posting window will appear with the title, text, and any embedded video or audio files already filled in, ready to go.
Or... you can just click this button to open the Pages posting window right away.
Last updated: 2023-04-04 11:11 am PDT
LGF User's Guide RSS Feeds

Help support Little Green Footballs!

Subscribe now for ad-free access!Register and sign in to a free LGF account before subscribing, and your ad-free access will be automatically enabled.

Donate with
PayPal
Cash.app
Recent PagesClick to refresh
Trump’s “Stolen Election” Lie Based on Evidence From Pervy Bathroom Cam-Spy OK, this really takes the cake. If you have relatives that still cling to the “election was stolen, dadgum, I jes’ KNOW IT … This should be a slight remedy to the stubborn madness Thanks to online anonymity, the ...
Khal Wimpo (free internal organs upon request!)
Yesterday
Views: 49 • Comments: 0 • Rating: 0
Best of April 2024 Nothing new here but these are a look back at the a few good images from the past month. Despite the weather, I was quite pleased with several of them. These were taken with older lenses (made from the ...
William Lewis
2 days ago
Views: 164 • Comments: 2 • Rating: 5
Texas County at Center of Border Fight Is Overwhelmed by Migrant Deaths EAGLE PASS, Tex. - The undertaker lighted a cigarette and held it between his latex-gloved fingers as he stood over the bloated body bag lying in the bed of his battered pickup truck. The woman had been fished out ...
Cheechako
3 weeks ago
Views: 408 • Comments: 0 • Rating: 1