Zend - The PHP Company




Text

Add Code


Cleanup Function  

Type: code fragment
Added by: RzE
Entered: 14/01/2001
Last modified: 04/12/2000
Rating: - (fewer than 3 votes)
Views: 7320
Cleans up a string. Changes certain characters into HTML compatable entities, removes many newlines, removes starting/trailing whitespace. removes multiple spaces. Finally, it makes it so users can't enter a stream of text to make the page's width screwy.


<?php
function cleanup ($comment)
/*
string cleanup (string string);

Cleans up string into HTML compatable code.

Note: You may need to change nr to n.
*/
{
    
$comment trim ($comment); // remove starting/trailing whitespace

    
for ($c 1$c <= 4$c++)
        
$comment str_replace ("nnn""nn"$comment); // makes many lines into one.

    
$comment str_replace ("n"''nl2br ($comment)); // n to <br>

    
list ($words) = array (explode (' '$comment)); // $words array formed
    
$comment '';
    foreach (
$words as $c => $word) {
        if (
strlen ($word) > 60 and !ereg ("[[|]|//|.com]"$word))
            
$word wordwrap ($word60'<br>'1);
        
$comment .= ' ' $word;
    }

    
$comment str_replace (array ('<br> '' <br>'' <br> '), '<br>'$comment);
    return 
trim ($comment);
}
?>


Usage Example


cleanup ($string);


Rate This Script





Search



This Category All Categories