PHP/HTML display hidden characters

后端 未结 5 1700
梦如初夏
梦如初夏 2021-01-02 20:41

So, I have a bunch of code that I\'m pulling from a column in MySQL. This code includes hidden characters, such as \"\\t\" and \"\\n\".

I\'m trying to get that raw c

5条回答
  •  甜味超标
    2021-01-02 21:16

    Add shlashes and preserve newline ;)

    // Simple example: replace all newlines with their character equivalent
    // AND preserve formating (the newline)
    $value = str_replace("\n", "\\n\n", $value);
    $value = str_replace("\t", "\\t\t", $value);
    echo nl2br(htmlentities($value)); // for HTML as output, with 
    for newlines echo "
    " . $value . "
    "; // for raw, preformated output

    if you have

    $value = "Hi\t\n there";
    

    the result is

    hi\t    \n
    there

    for the html output line.

提交回复
热议问题