1

I can read data to my angular app from the database but I am just given the static \n in my text instead of a new line. I am aware I am supposed to convert all \n occurrences to <br /> but I am failing to do this, even when using echo nl2br;

//extract from my php file
if($result = mysqli_query($con, $sql))   
{

  $animals = [];
  $cr = 0;
  while($row = mysqli_fetch_assoc($result))
  {

    $animals[$cr]['animal']  = $row['animal'];
    $animals[$cr]['t1'] = $row['title1'];
    $animals[$cr]['p1'] = $row['paragraph1'];
    $cr++;

}
echo nl2br($animals);
  echo json_encode($animals);

Below is my angular file

//extract from my animals.component.html file

    <div class="container" *ngFor="let animal of this.animals">

        {{animal.t1}}
        {{animal.p1}}
        {{animal.t2}}

    </div>

However, my output on the webpage (coming from animal.t1) is just the same text as the database entry:

Animals \n are \n fun \n .

I have tried numerous different things and just cannot seem to convert the \n's to <br>. Has anyone got any advice on this?

jdaz
  • 5,964
  • 2
  • 22
  • 34
Mr Man
  • 93
  • 9
  • 1
    https://www.php.net/manual/en/function.nl2br.php takes a string not an array. – AbraCadaver Jun 21 '20 at 17:46
  • @MrMan Furthermore, from the PHP manual: **Returns string with
    or
    inserted before all newlines (\r\n, \n\r, \n and \r).** So to be honest, there is actually no "replacement" being performed.
    – mickmackusa Jun 22 '20 at 03:35

2 Answers2

2

nl2br takes a string not an array. If you select the columns in the query it is much easier. Just map the row array:

  // SELECT animal, t1, p1 FROM table WHERE .....

  while($row = mysqli_fetch_assoc($result))
  {
      $animals[] = array_map('nl2br', $row);
  }
  echo json_encode($animals);

If Angular is converting HTML to entities then you may want to look here Using HTML Entities within Angular strings

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Use nl2br:

$animals[$cr]['t1'] = nl2br($row['title1']);
  • Thank you for your answer! it seems to work in changing it, however the print out on the webpage i am now getting is Animals
    are
    fun
    . Do you know why html isn't rendering these line breaks?
    – Mr Man Jun 21 '20 at 18:07
  • I don't know Angular but it is probably converting HTML entities so `<` to `<` which renders as `<` instead of creating an HTML tag. This is probably a security precaution. – AbraCadaver Jun 21 '20 at 18:28
  • While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 21 '20 at 22:47