[thelist] converting whitespace to single space between printable characters

Alex Beston alex at deltatraffic.co.uk
Wed Dec 7 09:12:29 CST 2005


Hi evolt

Im trying to reduce the whitespace between printable characters to a single space.

What i want to do is get all of the the words in a text so as to make a 
histogram of word frequencies.

Im doing this by exploding the string and using space as the delimiter. 
that only works in so far as words that are separated by spaces. so i 
find that i get words that have been yoked together since they were 
seperated with a return character.

how would i convert the text to be ready before i use 'explode'?

----
function getdetext($text){
   $buffer="";
   $handle = fopen($text, "r");
   if ($handle)
       while (!feof ($handle) )
           $buffer .= trim(fgets($handle, 4096));

    fclose ($handle);

    #$buffer = preg_replace('/[[:space:]]{2,}/', ' ',  $buffer); // convert before returning
    #$buffer = preg_replace('/[^[:print:]]/', ' ', $buffer); 

   return $buffer;
}


$readtext = getdetext($string);
echo $readtext;
$words = array();
$histogram = array();
$words = explode(' ', $readtext);

----
ta
Alex
 




More information about the thelist mailing list