0

I am trying to output this string returned to me via API

enter image description here

but when I print it using this

<?php echo $m["page_features"]; ?>

it is printed like folloing (single line)

Bunkhouse Front Bedroom Kitchen Island Loft Outdoor Kitchen 4 Slide-Outs

But I want it like

  • Bunkhouse
  • Front Bedroom
  • Kitchen Island
  • Loft
  • Outdoor Kitchen
  • 4 Slide-Outs

How can I achieve the result as desired output, please help

3
  • 1
    Have you try n2lbr? Commented Jun 11, 2021 at 19:47
  • 1
    It worked thanks @SimoneRossaini Commented Jun 11, 2021 at 19:57
  • You are welcome :) Commented Jun 11, 2021 at 21:49

2 Answers 2

1

Use nl2br for regular line breaks

<?php echo nl2br($m["page_features"]); ?>

for a UL list:

<?php 
$features = explode("\n",$m["page_features"]);
if (count($features)>0){
  echo"<ul>";
  foreach($features as $f) echo"<li>$f</li>";
  echo "</ul>";
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @Kinglish for giving the exact solution
0

Solution is use php explode function and forloop (any loop) to loop for each value ..
It would be really helpful if you provide complete code ! So taking 2 assumtions first the data recived has br tag and second it has too many whitespaces .... If i assume the tag used was
then try this one

    <?php
    
    $str = "Bunk house <br> Front Bedroom   <br> kitchen <br> island  <br> loft  <br>      etc";
    
    $array = array_filter(explode("<br>",$str));
    
    foreach ($array as $value){
       echo  "<li>".$value."</li><br>";
    }
    
    ?>

If I assume that there are too many whitespaces inside the string then code becomes

   <?php
    
    $str = "Bunk house  Front Bedroom    kitchen island   loft        etc";
    
    $array = array_filter(explode("  ",$str));
    
    foreach ($array as $value){
       echo  "<li>".$value."</li><br>";
    }
    
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.