我有一个这样的txt文件列表
dddd
ggggb
btbtgbgb
gtrbr
我希望每个项目都是这样的:
'dddd'
'ggggb'
'btbtgbgb'
'gtrbr'
答案 0 :(得分:1)
echo "'" . str_replace(' ', "' '", 'dddd ggggb btbtgbgb gtrbr') . "'";
// returns 'dddd' 'ggggb' 'btbtgbgb' 'gtrbr'
答案 1 :(得分: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.
答案 2 :(得分: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);
注意:这可能不会很好地扩展。