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

2019-03-03 13:38发布

问题:

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.

回答1:

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


回答2:

$str = 'asdf,asdf';
$str = str_replace(',','<br />',$str);


回答3:

Do a php str_replace.

$final_data = str_replace(",","<br />",$row["data"]);


回答4:

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:

Just use str_replace

$my_output = str_replace(",", "<br />", "no,thanks,text,text");


回答6:

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