Long word Filter

Discussion in 'PHP' started by Silver89, Jul 30, 2008.

  1. #1
    Is there a function in php or css to stop people posting huge long words.

    If someone posts a word on my forum that has a huge amount of charaters then it will keep going on one line forever, can this be stopped?

    Thanks
     
    Silver89, Jul 30, 2008 IP
  2. jack_ss

    jack_ss Guest

    Messages:
    94
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this PHP code:

    
    $char_limit = 30; // number of characters allowed
    $words = preg_split("/[\s,]+/", $text);
    if (is_array($words))
    	foreach ($words as $v)
    		if (strlen($v) > $char_limit) {
    			// do something because a word has more than specified number of characters.
    		}
    PHP:
    Best o' luck.
     
    jack_ss, Jul 30, 2008 IP
    Silver89 likes this.
  3. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #3
    How would I insert a space after every 24 characters using this?
     
    Silver89, Jul 30, 2008 IP
  4. jack_ss

    jack_ss Guest

    Messages:
    94
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here goes, as a function.. a little sloppy, but it works:

    
    // Usage: <?= long_word_spacer($comment_text, $character_limit); ?>
    
    function long_word_spacer($text, $char_limit = 24) {
    	$words = preg_split("/[\s,]+/", $text);
    	if (is_array($words))
    	    foreach ($words as $v) {
    	        $r = "";
    			if (strlen($v) > $char_limit) {
    				for ($i = 0; $i < strlen($v); $i++) {
    					if ($c++ == $char_limit) {
    						$r .= $v[$i]." ";
    						$c = 0;
    					} else $r .= $v[$i];
    				}
    				$text = str_replace($v, $r, $text);
    	        }
    		}
    	return $text;
    }
    PHP:
    You can replace the " " with a "<br>" or "\n" if you feel so inclined.
     
    jack_ss, Jul 30, 2008 IP
  5. jack_ss

    jack_ss Guest

    Messages:
    94
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I totally forgot about PHP's word_wrap function (http://us2.php.net/wordwrap). Check out this cleaner variation of the same function:

    
    function long_word_spacer($text, $char_limit = 24) {
    	$words = preg_split("/[\s,]+/", $text);
    	if (is_array($words)) {
    		foreach ($words as $k => $v) {
    			if (strlen($v) > $char_limit) $v = wordwrap($v, $char_limit, " ", true);
    			$r .= $v." ";
    		}
    		return $r;
    	} else {
    		return wordwrap($text, $char_limit, " ", true);
    	}
    }
    
    PHP:
    There you go.
     
    jack_ss, Jul 30, 2008 IP
  6. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #6
    Perfect Thanks :)
     
    Silver89, Jul 30, 2008 IP
  7. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #7
    function word_split($text, $char_limit = 24) {
        
        $words =  str_word_count($text, 1);
        
        foreach ($words as $v) {
            
            $r .= (strlen($v) > $char_limit)? wordwrap($v, $char_limit, ' ', true).' ':$v.' ';  
    
        }//end for loop
        
        return $r;
        
    }//end word_split function
    
    
    PHP:
     
    php-lover, Jul 30, 2008 IP
    jack_ss and Silver89 like this.
  8. jack_ss

    jack_ss Guest

    Messages:
    94
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Very cool php-lover. I've never used str_word_count before. Props.
     
    jack_ss, Jul 31, 2008 IP