在动态字符串中添加换行符?

时间:2016-07-04 03:23:16

标签: php

我有一些来自数据库的动态字符串。如何插入换行符看起来像这样:NOte:我无法编辑字符串

自:

From Wednesday, Jul 6, 2016 till Thursday, Jul 7, 2016 (1 night): 1 x Deluxe Room - Double (+ 1 extra adult) and 1 x Deluxe Room - Twin (2 Adults, 2 Children). Food arrangement: Breakfast
From Saturday, Jul 2, 2016 till Sunday, Jul 3, 2016 (2 nights): 3 x Deluxe Room - Double (+ 1 extra child) (2 Adults, 2 Children). Food arrangement: Breakfast

要:

From Wednesday, Jul 6, 2016 till Thursday, Jul 7, 2016 (1 night).
1 x Deluxe Room - Double (+ 1 extra adult)
1 x Deluxe Room - Twin (2 Adults, 2 Children)
Food arrangement: Breakfast

From Saturday, Jul 2, 2016 till Sunday, Jul 3, 2016 (2 nights).
3 x Deluxe Room - Double (+ 1 extra child) (2 Adults, 2 Children)
Food arrangement: Breakfast

2 个答案:

答案 0 :(得分:1)

$str="From Wednesday, Jul 6, 2016 till Thursday, Jul 7, 2016 (1 night): 1 x Deluxe Room - Double (+ 1 extra adult) and 1 x Deluxe Room - Twin (2 Adults, 2 Children). Food arrangement: Breakfast
From Saturday, Jul 2, 2016 till Sunday, Jul 3, 2016 (2 nights): 3 x Deluxe Room - Double (+ 1 extra child) (2 Adults, 2 Children). Food arrangement: Breakfast"
;

$output="";
$arr=explode("From", $str);
for ($i=1; $i<count($arr); $i++){
$temp=explode(":", $arr[$i]);
$start="From ".$temp[0].".\r\n";
$rm=str_replace("Food arrangement", "", $temp[1]);
$rooms=explode("and", $rm);
$end="Food arrangement: ".$temp[2]."\r\n";
$output.=$start;
foreach ($rooms as $room){
        $output.= $room."\r\n";
}
$output.=$end;
}
echo $output;

答案 1 :(得分:0)

<?php


$str="From Wednesday, Jul 6, 2016 till Thursday, Jul 7, 2016 (1 night): 1 x Deluxe Room - Double (+ 1 extra adult) and 1 x Deluxe Room - Twin (2 Adults, 2 Children). Food arrangement: Breakfast
From Saturday, Jul 2, 2016 till Sunday, Jul 3, 2016 (2 nights): 3 x Deluxe Room - Double (+ 1 extra child) (2 Adults, 2 Children). Food arrangement: Breakfast"
;

echo $str ."<br><br><br>";
$new_string=str_replace(".",".\n",$str);

echo nl2br($new_string);


?>