如何在PHP中的所有换行符之前和之后在span中包装字符串? [英] How to wrap string in span before and after all newlines in PHP?

查看:38
本文介绍了如何在PHP中的所有换行符之前和之后在span中包装字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在搜索后,我发现PHP函数确实在字符串中的所有换行符之前插入

Upon searching I found PHP function that do inserts
before all newlines in a string which is

nl2br();

示例:

<?php
   echo nl2br("This is an example\r\n where line breaks\r\n added", false);
?>

以上代码输出:

This is an example<br\>
where line breaks<br\> 
added

我想要输出而不是< br/>我将在所有换行符之前和之后用标签将字符串包装起来

What I wanted to have output instead of <br/> I will wrap the string with the tags before and after all newlines

示例代码从带有跨度的换行符上方的代码输出

<span>This is an example</span>
<span>where line breaks</span> 
<span>added</span>

是否存在PHP函数?或自定义PHP函数

Is there PHP function exist to this? or a custom PHP function

推荐答案

    $str = "This is an example\r\n where line breaks\r\n added";
    $str = explode("\r\n",$str);
    foreach($str as $key => $value) {
       echo "<span>".$value."</span>";
}

这篇关于如何在PHP中的所有换行符之前和之后在span中包装字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆