欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

php 把换行转换为</br> 回车转换行 有大用

shiping1 的头像

2011-02-21 11:53:48|  分类: PHP资料 |  标签:换行转换为<br>  |举报|字号 订阅

下载LOFTER客户端
 
 
$message = ereg_replace("\n", "<BR>\n", $message);
也可写成:

echo nl2br($message);

ereg_replace

字符串比对解析并取代。

语法: string ereg_replace(string pattern, string replacement, string string);

返回值: 字符串

函数种类: 资料处理

 

 

 
内容说明

 

本函数以 pattern 的规则来解析比对字符串 string,欲取而代之的字符串为参数 replacement。返回值为字符串类型,为取代后的字符串结果。

 

 

 
使用范例

 

ken@freebsdrocks.com 在 16-Mar-1999 提出的例子。

<?php
$text 
'This is a {1} day, not {2} and {3}.';
$daytype = array( => 'fine',
                  
=> 'overcast',
                  
=> 'rainy' );
while (
ereg ('{([0-9]+)}'$text$regs)) {
  
$found $regs[1];
  
$text ereg_replace("\{".$found."\}"$daytype[$found], $text);
}
echo 
"$text\n";
// This is a fine day, not overcast and rainy. 
?>

ken@freebsdrocks.com 并同时提出具有相同功能的perl 程序范例如下:

$text = 'This is a {1} day, not {2} and {3}.';
%daytype = ( 1 => 'fine',
             2 => 'overcast',
             3 => 'rainy' );
$text =~ s/{(\d+)}/$daytype{$1}/eg;
print "$text\n";

nl2br

将换行字符转成 <br>。

语法: string nl2br(string string);

返回值: 字符串

函数种类: 资料处理

 

 

 
内容说明

 

本函数将换行字符转换成 HTML 换行的 <br> 指令。

来自 http://blog.163.com/wz_pk007/blog/static/1706270502011121115331388/

普通分类: