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

为君一笑 提交于 2019-12-02 20:11:47

问题


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


来源:https://stackoverflow.com/questions/6230515/how-do-i-read-a-as-br-in-php-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!