Jump to content

Words of a certain length


Tandem

Recommended Posts

Hi,

 

my site features a lot of user input, such as user profiles, and some forums that i wrote myself as well as many other similar examples. My problem is that if somebody enters a very long word, wider than the page width, it sort of screws up the page design, and generally causes me some annoyance whilst performing certain tasks.

 

I'm wondering if there is a way to detect whether a string has any words longer than a specified length in it?

 

I was thinking that perhaps using explode(" ", $variable), and then looping through the array and making sure that there are no words of longer than the length i want, but i can see that being quite slow

 

Any suggestions or links to any help are appreciated.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/49011-words-of-a-certain-length/
Share on other sites

[quote=www.php.net/wordwrap]
Peter
19-Dec-2006 05:00 
The main concern when you have a text in a cell is for long words that drags the cell margins. This function will break words in a text that have more then $nr characters using the "-" char.

function processtext($text,$nr=10)
    {
        $mytext=explode(" ",trim($text));
        $newtext=array();
        foreach($mytext as $k=>$txt)
        {
            if (strlen($txt)>$nr)
            {
                $txt=wordwrap($txt, $nr, "-", 1);
            }
            $newtext[]=$txt;
        }
        return implode(" ",$newtext);
    } 

<?php
function processtext($text,$nr=10)
    {
        $mytext=explode(" ",trim($text));
        $newtext=array();
        foreach($mytext as $k=>$txt)
        {
            if (strlen($txt)>$nr)
            {
                $txt=wordwrap($txt, $nr, "-", 1);
            }
            $newtext[]=$txt;
        }
        return implode(" ",$newtext);
    } 
?>

 

Shouldn't be slow at all. I bet it can process 10,000 words fairly quickly. Give it a test and see if that works out for you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.