Ala*_*air 0 php trim preg-replace
为什么这不起作用?
$content=file_get_contents('whatever.txt');
$content=str_replace("\r\n","\n",$content); // Convert linebreaks
$content=str_replace("\n",' ',$content); // Remove linebreaks
$content=preg_replace('/\s+/',' ',$content); // Remove all duplicate spaces
$content=preg_replace('/[^a-zA-Z\s]/','',$content); // Remove all non-letters
$content=trim($content);
$content=explode(' ',$content);
Run Code Online (Sandbox Code Playgroud)
有些值仍然是空白:
例如
$content[123] = '';
Run Code Online (Sandbox Code Playgroud)
我是否理解错误?肯定应该没有吧?
我认为整个事情可以总结/修正为以下几行:
$content = file_get_contents('whatever.txt'); // Get file
$content = preg_replace('/[^a-zA-Z\s]/', '', $content); // Strip non-alpha/whitespace
$content = preg_split('/\s+/', $content, NULL, PREG_SPLIT_NO_EMPTY); // Split by whitespace and remove empty elements
Run Code Online (Sandbox Code Playgroud)