I have extracted the text from a PDF file and some of the text has extra whitespaces between words.
Your water a n d wastewater s t a t e m e n t
I wrote a function to remove the extra spaces from the text above.
function removeExtraWhitespace($val) {
$nval = "";
for($i = 0; $i < strlen($val); $i++) {
if($val[$i] != " ") {
$nval .= $val[$i];
}
else if((isset($val[$i-2]) && $val[$i-2] != " ") || (isset($val[$i+2]) && $val[$i+2] != " ")) {
$nval .= $val[$i];
}
}
return $nval;
}
Which will output:
Your water and wastewater statement
I know that this function will not work in all circumstances though. If the text has a valid 1 letter word, like 'a', then it will fail, or if only part of a word has extra spaces.
I n e e d to remove whitespaces f r o m a string
When putting the above text in to my function it will output:
Ineed to remove whitespaces froma string
Is there a way to make a function that will work on all possible text?