0

I have an index.php file and an array which has message. Is there a way that instead of <br> tag I can display the text with a new line in PHP so I can also store it in database?

The code:

$array = array(
  array(
    'id' => 1,
    'message' => 'I\'m reading Harry Potter!',
  ),
  array(
    'id' => 2,
    'message' => 'Ok. I just got a notification that you sent me a pin on Pinterest.<br>Will you come to school tomorrow?',
  )
);

For example:

Ok. I just got a notification that you sent me a pin on Pinterest.
Will you come to school tomorrow?

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
  • 2
    No, I'm not going to school. But you could perhaps use `\n` or any combination you want. Say `€¥|€¥€¢€||÷°™{¶{¥` means new line. The just explode on this pattern and you have your lines. – Andreas Jan 21 '19 at 19:57
  • 1
    Just use `\n\n` and then when you want to display it use `nl2br()`. – AbraCadaver Jan 21 '19 at 20:00
  • Your question seems to be somehow related to DB storage, but I can't see any code which deals with DB. Do you have an actual problem inserting newline characters into MySQL? – Dharman Jan 21 '19 at 20:03
  • @Alessio Cantarella You have removed a newline break from question which changes its meaning! – Dharman Jan 21 '19 at 20:10
  • 1
    @Dharman he removed one line break that was incorrectly placed in the expected output. What you see now is correct. One be tag makes one line break, not two as it was. – Andreas Jan 21 '19 at 20:18
  • @Dharman im creating a page like chat and I want it to store it in DB the message from the array but I haven't written any code in DB yet..For the moment even in displaying the array message it doesn't work with \n or "\n" for example.. – C new coder Jan 21 '19 at 20:24
  • How are you displaying the message? Could you show us a little more code, please? – Dharman Jan 21 '19 at 20:27
  • =$array['message']?> I have done it using a template file .php and and using a loop in index.php to include it – C new coder Jan 21 '19 at 20:31
  • str_replace('
    ', "\n", $string)
    – Andrew Jan 21 '19 at 21:01

3 Answers3

2

The new line character is \n. Simply replace <br> with \n and you will have the results you're looking for.

PHP - how to create a newline character?

Note!

php does not process escape characters within single quotes.

'\n' is not processed as a new line character, while "\n" is.

What is the difference between single-quoted and double-quoted strings in PHP?

Depending on your platform, you may want to be more specific about which new line sequence you choose.

\r\n, \r and \n what is the difference between them?

$array = array(
  array(
    'id' => 1,
    'message' => "I'm reading Harry Potter!"
  ),
  array(
    'id' => 2,
    'message' => "Ok. I just got a notification that you sent me a pin on Pinterest.\nWill you come to school tomorrow?"
  )
);

echo "<pre>";
print_r($array);
exit;

This will show you the raw formatting of your string.

To then convert new lines to the <br> tag for display on a webpage, you would pass that string to nl2br()

<?php echo nl2br($array[1]['message']); ?>
Kenneth
  • 535
  • 2
  • 17
  • When I use \n it is displayed with that too ..For example:Ok. I just got a notification that you sent me a pin on Pinterest.\nWill you come to school tomorrow? With double quotes is the same.. – C new coder Jan 21 '19 at 20:19
  • See the edits to my answer and test it that way – Kenneth Jan 21 '19 at 20:28
  • This is pedantic, but nl2br() can handle all line endings (\r, \n, \r\n, \n\r) I tend to prefer to use PHP_EOL in my code because this holds the line ending for the current environment/platform (eg, Windows or OSX). – DavidScherer Jan 21 '19 at 20:48
  • To display the message... I have done it using a template file .php and and using a loop in index.php to include it

    =$array['message']?>

    so can I do it without echo?Is there another way to write it ..?
    – C new coder Jan 21 '19 at 20:52
  • Sure is! `=` is called the short hand echo. `=nl2br($array['message'])?>` will work just fine. – Kenneth Jan 22 '19 at 03:07
1

Is there a way that instead of <br> tag I can display the text with a new line in PHP

Yes you can easily do this with CSS

white-space: pre;

https://www.w3schools.com/cssref/pr_text_white-space.asp

Back in the day I used to do the whole "replace" thing, then I got bored of it. Now I just use CSS.

The pre option/setting will preserve whitespace much like using the <pre> tag. The only thing you have to watch for is indenting in the source code

<p style="white-space:pre;">
   <?php echo $something; ?>
</p>

This extra space in the code will be added to the PHP output, instead do this:

<p style="white-space:pre;"><?php echo $something; ?></p>
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
-4

You can close the php tag and reopen after the break.

For example -

'message'=>'Ok.I just got a notification that you sent me a pin on Pinterest. ?>
<br>  
<?php Will you come to school tomorrow?', 
Andreas
  • 23,610
  • 6
  • 30
  • 62
DG 33
  • 1
  • 1
    this is a REALLY unelegant, ugly way to solve this – JoSSte Jan 21 '19 at 20:10
  • @JoSSte How does it solve anything? It is a string in PHP, you can't escape PHP into HTML and expect the result to magically appear inside of the string. – Dharman Jan 21 '19 at 20:26
  • A solution like using double quotes and then using escape sequences, or other placeholders is much more elegant. jumping in and out of PHP with `?>` and ` – JoSSte Jan 21 '19 at 20:47