Php将新行显示为html文本


Php display as a html text the new line

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

我想知道结果是''n或''t或''r,以及有多少。我需要知道,这样我就可以用<p><div>这样的html标记来替换它。

结果来自其他网站。

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: 

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

如果您想让''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);

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

$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 '*'