2011-09-20 57 views
0

我長這樣添加字符

dddd 
ggggb 
btbtgbgb 
gtrbr 

txt文件列表,我想每一個項目是這樣的:

'dddd' 
'ggggb' 
'btbtgbgb' 
'gtrbr' 

回答

1
echo "'" . str_replace(' ', "' '", 'dddd ggggb btbtgbgb gtrbr') . "'"; 
// returns 'dddd' 'ggggb' 'btbtgbgb' 'gtrbr' 
+0

更好的答案? –

0
$str = "dddd ggggb btbtgbgb gtrbr"; 
    echo "'" . str_replace(' ', "' '", preg_replace('/\s\s+/', ' ', $str)) . "'"; 
    //returns 'dddd' 'ggggb' 'btbtgbgb' 'gtrbr' and strips extra whitespace if ever your text file has extra whitespaces. 
0
// load file into an array 
$lines = file($textfile); 
foreach ($lines as $key => $line) { 
     // add quotes and remove tailing newline 
     $lines[$key] = "'".rtrim($line, "\n")."'"; 
} 
print_r($lines); 

注意:這可能不會很好地擴展。