如何在字符串中的每个符号后面创建一个新行?

时间:2014-08-08 01:51:14

标签: php

我有一个类似这样的字符串:

This is line 1.* This is line 2.* This is line 3.

它的格式应该是这样的:

This is line 1.
This is line 2.
This is line 3.

我在想这必须使用substr()或类似的东西来完成,但我不知道。

3 个答案:

答案 0 :(得分:4)

您可以使用str_replacenl2br功能。

<?php 
    $str = "This is line 1.* This is line 2.* This is line 3.";
    echo nl2br(str_replace("*", "\n", $str));    
?>

答案 1 :(得分:3)

这将为您完成:)

$s = "This is line 1.* This is line 2.* This is line 3.";

$d = implode("\n", explode('* ', $s));

返回此内容:

This is line 1.
This is line 2.
This is line 3.

Example


或者,您可以使用str_replace()执行此类操作:

$s = "This is line 1.* This is line 2.* This is line 3.";

$s = str_replace("* ", "\n", $s);

Example

答案 2 :(得分:-1)

使用explode()函数拆分此字符串。