Php以html文本显示新行\ n

问题描述 投票:0回答:2

我正在使用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: 
php html line-breaks pre
2个回答
1
投票

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

如果要在输出中显示\ 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;


0
投票

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

str_replace("\t", '&emsp;', $string);
© www.soinside.com 2019 - 2024. All rights reserved.