Hi Guys,
Say I got a text file.
Of course, the text file will contain a lot of spaces.
I want to delete about half of those spaces.
How do I do that?
$pos = -1; while ((++$pos < strlen($str)) && (($pos = strpos($str, ' ', $pos)) !== false)) { if (mt_rand(false, true)) { $str{$pos} = ''; } }
If you don't really want to get rid of random space, but rather excess space, you could do something like:
$search = array('/[ \t]+/', '/[\r\n]+/'); $replace = array(' ', "\n"); $text = preg_replace($search, $replace, $text);