开发者

Switching from Space to Tab delimited in php

I have the following bit of code:

// 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","Manuf开发者_如何学JAVAacturer 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']);

As you can see it is using spaces. Is there an easy way to switch the over to using tabs? I tried dooing this:

/ 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']);

 }

Aparently off. o_O


str_replace(' ',"\t",$string);


If you mean you want to output tabs instead of spaces:

// 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']);


If there are no literal spaces in the values, then simply using str_replace() should cut it.

str_replace(' ', "\t", $line);


Might have misunderstood, but if you want to change fprintf's left justification character to tab instead of space, you can use

%'\t-10s

instead of

%-10s

If you want to replace the spaces between words with tabs, just do what others suggested here, 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");

If you want to make this a bit nicer, you can also use str_repeat, so you don't need such a long format string.

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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜