user3064132
user3064132

Reputation: 113

add a line break to dynamic string?

I have some dynamic string get from database . how can i insert a line break to look like this : NOte : i can't edit string

From:

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

To:

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

Upvotes: 1

Views: 795

Answers (2)

CHIN
CHIN

Reputation: 1

<?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);


?>

Upvotes: 0

SML
SML

Reputation: 1265

$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;

Upvotes: 1

Related Questions