我有以下代码:
// Loop through returned data and write (append) directly to file
fprintf($fh, "%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s 'n", "ID", "Name", "Description","Price","Merchant Category","URL","Image URL","Manufacturer","Manufacturer Part Number","Brand","Keywords","Quantity","Weight","UPC");
fprintf($fh, "'n");
while ($row = mysql_fetch_assoc($result)) {
fprintf($fh, "%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s 'n", $row['card_id'], $row['TitleConcat'], $row['description'], $row['price'], $row['Merchant'], $row['link'], $row['ImageURL'], $row['artist'], $row['Part'], $row['Brand'], $row['seo_keywords'], $row['quantity'], $row['Weight'], $row['UPC']);
正如你所看到的,它正在使用空格。有没有一种简单的方法可以将切换到使用选项卡?我试过这样做:
/ Loop through returned data and write (append) directly to file
fprintf($fh, "'t 't 't 't 't 't 't 't 't 't 't 't 't 't 'n", "ID", "Name", "Description","Price","Merchant Category","URL","Image URL","Manufacturer","Manufacturer Part Number","Brand","Keywords","Quantity","Weight","UPC");
fprintf($fh, "'n");
while ($row = mysql_fetch_assoc($result)) {
fprintf($fh, "'t 't 't 't 't 't 't 't 't 't 't 't 't 't 'n", $row['card_id'], $row['TitleConcat'], $row['description'], $row['price'], $row['Merchant'], $row['link'], $row['ImageURL'], $row['artist'], $row['Part'], $row['Brand'], $row['seo_keywords'], $row['quantity'], $row['Weight'], $row['UPC']);
}
完全关闭.o-o
str_replace(' ',"'t",$string);
如果您想输出选项卡而不是空格:
// Loop through returned data and write (append) directly to file
fprintf($fh, "%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s 'n", "ID", "Name", "Description","Price","Merchant Category","URL","Image URL","Manufacturer","Manufacturer Part Number","Brand","Keywords","Quantity","Weight","UPC");
fprintf($fh, "'n");
while ($row = mysql_fetch_assoc($result)) {
fprintf($fh, "%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s't%-10s 'n", $row['card_id'], $row['TitleConcat'], $row['description'], $row['price'], $row['Merchant'], $row['link'], $row['ImageURL'], $row['artist'], $row['Part'], $row['Brand'], $row['seo_keywords'], $row['quantity'], $row['Weight'], $row['UPC']);
如果值中没有文字空间,那么只需使用str_replace()就可以将其截断。
str_replace(' ', "'t", $line);
可能有误解,但如果您想将fprintf
的左对齐字符更改为制表符而不是空格,则可以使用
%''t-10s
而不是
%-10s
如果您想用制表符替换单词之间的空格,只需按照其他人的建议执行即可,str_replace()
:
$s="%-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s %-10s 'n";
fprintf($fh, str_replace(' ', "'t", $s), "ID", "Name", "Description","Price","Merchant Category","URL","Image URL","Manufacturer","Manufacturer Part Number","Brand","Keywords","Quantity","Weight","UPC");
如果你想让它变得更好,你也可以使用str_repeat
,所以你不需要这么长的格式字符串。
fprintf($fh, str_repeat("%-10s't", 14)."'n", "ID", "Name", "Description","Price","Merchant Category","URL","Image URL","Manufacturer","Manufacturer Part Number","Brand","Keywords","Quantity","Weight","UPC");