0

I'm tring create a comment by Wordpress Rest API and I have a problem.

Creating works but I can't save comment with break lines.

In database I have:

line 1\nline 2\nline 3

What can I do before post request with text?

Is there any function in php that will add break lines instead of \n?

@edit

This is my curl post:

$parameters = '?post=' . $this->request->getVar('eventId') . '&content=' . $this->request->getVar('content');

            if ($this->request->getVar('authorName')) {
                $parameters .= '&author_name=' . $this->request->getVar('authorName');
            }

            try {
                $apiComment = $curl->post('https://xxxx.xx/wp-json/wp/v2/comments' . $parameters, [
                    'headers' => [
                        'Accept' => 'application/json'
                    ]
                ]);

This is from another api and it's string:

$this->request->getVar('content')

enter image description here

1 - from www 2 - from wp rest api

I want the same as 1.

Lidia K.
  • 209
  • 3
  • 13
  • 1
    Are you looking for [nl2br](https://www.php.net/manual/en/function.nl2br.php)? `\n` is a regular line break, but it's not one that HTML recognizes. – aynber Oct 05 '22 at 19:01
  • 1
    @aynber has the right answer. There is no interpretation of a newline in html. You have to have a
    tag to break lines. However if you are using a textarea for display, you should look at this answer, in particular the css solution: https://stackoverflow.com/questions/8627902/how-to-add-a-new-line-in-textarea-element
    – gview Oct 05 '22 at 19:09
  • I don't mean displaying just a insert of record in the database. When I use nl2br nothing happens. – Lidia K. Oct 05 '22 at 19:12
  • Oh, you don't want new lines in your URL at all, either with `\n` or `
    `. You'll want to encode it with https://www.php.net/manual/en/function.htmlspecialchars.php , since it looks like it's accepting json, it might be better to create an array and json_encode it. See https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl for directions on that
    – aynber Oct 05 '22 at 19:12
  • It helps: str_replace(['\n'], '%0D%0A', $this->request->getVar('content')) – Lidia K. Oct 06 '22 at 10:29
  • 1
    @LidiaK. If you found an answer to your problem, you may [answer your own question](https://meta.stackoverflow.com/questions/250204/can-you-answer-your-own-questions-on-stack-overflow) instead of submitting an answer in the question description. Check [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – steven7mwesigwa Oct 06 '22 at 11:18

1 Answers1

0

It's solution:

str_replace(['\n'], '%0D%0A', $this->request->getVar('content'))
Lidia K.
  • 209
  • 3
  • 13
  • Thanks for posting the solution you discovered, Lidia. I personally find it really helpful and thoughtful when the person who posted the original question also posts the resolution once it's discovered - it really helps. – quinny Oct 11 '22 at 22:56