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

前端 未结 6 557
傲寒
傲寒 2021-01-27 10:55

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 \"
\" inste

相关标签:
6条回答
  • 2021-01-27 11:26
    $str = 'asdf,asdf';
    $str = str_replace(',','<br />',$str);
    
    0 讨论(0)
  • 2021-01-27 11:34

    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 );
    
    0 讨论(0)
  • 2021-01-27 11:41

    Do a php str_replace.

    $final_data = str_replace(",","<br />",$row["data"]);
    
    0 讨论(0)
  • 2021-01-27 11:42

    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
    
    0 讨论(0)
  • 2021-01-27 11:44

    Just use str_replace

    $my_output = str_replace(",", "<br />", "no,thanks,text,text");
    
    0 讨论(0)
  • 2021-01-27 11:44

    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'");
    
    0 讨论(0)
提交回复
热议问题