How do I read a “,” as “
” in PHP/MySQL?

2019-03-03 13:27发布

So I have this MySQL database and a table and in the rows there a lot of "," in them, and I wish when they are output on the screen with PHP to be switched to "
" instead of coma, how do I do that?

I mean, this is a example, it stands like this: hello,no,thanks and instead of being output like that I would like it to be output as: hello no thanks

How do I do that? Could someone do it for me? Would be very friendly.

6条回答
ゆ 、 Hurt°
2楼-- · 2019-03-03 13:46

Just use str_replace

$my_output = str_replace(",", "<br />", "no,thanks,text,text");
查看更多
淡お忘
3楼-- · 2019-03-03 13:51
$str = 'asdf,asdf';
$str = str_replace(',','<br />',$str);
查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-03 13:51

Use str_getcsv() to make sure the escaped commas are processed correctly.

Code:

$input = "hello,no,thanks";
$array = str_getcsv($input);
$result = implode("\n",$array);

Output

hello
no
thanks
查看更多
时光不老,我们不散
5楼-- · 2019-03-03 13:59

Do a php str_replace.

$final_data = str_replace(",","<br />",$row["data"]);
查看更多
Lonely孤独者°
6楼-- · 2019-03-03 14:04

You could try the MySQL REPLACE function to have the data come back from your query already formatted like this:

Example:

select replace(column_with_commas_in_strings,',','<br />') as new_string
from some_table;

In your case, something like:

$result = mysql_query("SELECT replace(alias,',','<br />') as ALIAS FROM characters WHERE namn = 'Jargon'");
查看更多
在下西门庆
7楼-- · 2019-03-03 14:07

Assuming there are no CSV quoting issues:

$newStr = str_replace( ',', '<br>', $string );

EDIT:

After seeing your rollback, i see that you actually want to replace the , with a newline character such as \r, \n or \r\n:

$newStr = str_replace( ',', "\n", $string );
查看更多
登录 后发表回答