Removing long words from a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Toneboy
Forum Contributor
Posts: 102
Joined: Wed Jul 31, 2002 5:59 am
Location: Law, Scotland.
Contact:

Removing long words from a string

Post by Toneboy »

Come across a problem on one of my sites with a feed. One of my users has put in a post where for the purposes of underlining she has put in umpteen "-" characters (where one <hr> would have sufficed, alas). As a result of this the format on the front page is temporarily knocked out of alignment.

This data is held within a MySql database, and a number of conditions are put on the parsed text already, e.g. strip_tags.

What would be the best answer to this problem?

So far I've considered:

1) Cheating by using the wordwrap command.
2) Cheating by using ereg_replace or preg_replace to take the "-" character out of the feed.

But I figure there must be a better way to do this, just haven't been able to find it. Can anyone suggest one?

P.S. I would probably make a limit of twenty characters to a word, that seems a reasonable amount.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

html Area

Post by anjanesh »

If you are considering style textarea you might also want to try out this html Area :
http://www.interactivetools.com/products/htmlarea/
Its amazing with lots of functionality including <HR>.
User avatar
Toneboy
Forum Contributor
Posts: 102
Joined: Wed Jul 31, 2002 5:59 am
Location: Law, Scotland.
Contact:

Post by Toneboy »

Got this tip from a friend of mine:

Code: Select all

// In this example words 25 characters or longer are not shown.
$newstext = preg_replace("/[a-zA-Z0-9]{25,}/s", "", $newstext);
Post Reply