代码之家  ›  专栏  ›  技术社区  ›  Jows

Php将新行显示为html文本\n

  •  0
  • Jows  · 技术社区  · 11 年前

    我正在使用echo显示结果,但结果包含换行符/n/t/r。

    我想知道结果是\n或\t还是\r以及有多少。我需要知道,这样我就可以在html标记中替换它,比如 <p> <div> .

    结果来自其他网站。

    In pattern CreditTransaction/CustomerData: 
    
    
    
            Email does not contain any text
    
    In pattern RecurUpdate/CustomerData:      
    
    
    
        Email does not contain any text
    
    In pattern AccountInfo: 
    

    我想要这样。

    In pattern CreditTransaction/CustomerData: 
        \n
        \n
        \n  
          \n\tEmail does not contain any text
          \n
    In pattern RecurUpdate/CustomerData:      
        \n
        \n
          \n
        \n\tEmail does not contain any text
    
    \n\tIn pattern AccountInfo: 
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   0x47686F7374    11 年前

    你的问题很不清楚,但我会尽力提供答案。

    如果要使\n、\r和\t在输出中可见,您只需手动取消隐藏它们即可:
    str_replace("\n", '\n', str_replace("\r", '\r', str_replace("\t", '\t', $string)));

    或者,如果要取消隐藏所有转义字符:
    addslashes($string);

    要计算特定字符/子字符串出现的次数,请执行以下操作:
    substr_count($string, $character_or_substring);

    要检查字符串是否包含特定字符/子字符串,请执行以下操作:
    if (substr_count($string, $character_or_substring) > 0) { // your code }
    或:
    if (strpos($string, $character_or_substring) !== false) { // notice the !== // your code }

    正如前面其他人在评论中提到的,如果要将换行符转换为br标记:
    nl2br($string);

    如果要使选项卡缩进,可以将所有选项卡替换为 &emsp; :
    str_replace("\t", '&emsp;', $string);

        2
  •  0
  •   Wezy    11 年前

    使用双引号查找换行符和制表符。

    $s = "In pattern CreditTransaction/CustomerData: 
    
    
    
        Email does not contain any text
    
      In pattern RecurUpdate/CustomerData:  ";
    
    echo str_replace("\t", "*", $s);  // Replace all tabs with '*'
    echo str_replace("\n", "*", $s);  // Replace all newlines with '*'
    
    推荐文章