php – Newline in Javascript doesn´t work – \n \r <br>-ThrowExceptions

Exception or error:

I have a booking form which uses a function (attendees_full_info) to output all the data entered into an e-mail. Now the formatting bothers me because all labels and input are in one line.

I have the following code:

if(is_array($reg_form) and count($reg_form))
        {
            foreach($reg_form as $field_id=>$value)
            {
                // Placeholder Keys
                if(!is_numeric($field_id)) continue;

                $type = $reg_fields[$field_id]['type'];

                $label = isset($reg_fields[$field_id]) ? $reg_fields[$field_id]['label'] : '';
                if(trim($label) == '') continue;

                if($type == 'agreement')
                {
                    $label = sprintf(__($label, 'mec'), '<a href="'.get_the_permalink($reg_fields[$field_id]['page']).'">'.get_the_title($reg_fields[$field_id]['page']).'</a>');
                    $attendees_full_info .= $label.': '.($value == '1' ? __('Yes', 'mec') : __('No', 'mec'))."\r\n";
                }
                else
                {
                    $attendees_full_info .= __($label, 'mec').': '.(is_string($value) ? $value : (is_array($value) ? implode(', ', $value) : '---'))."\r\n";
                }
            }
        }

The result is the following:

Result 1

Here’s what I’ve already tried. After the “:” i have inserted \r\n or only \n. But this does not work.

example:

if($type == 'agreement')
                {
                    $label = sprintf(__($label, 'mec'), '<a href="'.get_the_permalink($reg_fields[$field_id]['page']).'">'.get_the_title($reg_fields[$field_id]['page']).'</a>');
                    $attendees_full_info .= $label.': \r\n'.($value == '1' ? __('Yes', 'mec') : __('No', 'mec'))."\r\n";
                }
                else
                {
                    $attendees_full_info .= __($label, 'mec').': \r\n'.(is_string($value) ? $value : (is_array($value) ? implode(', ', $value) : '---'))."\r\n";
                }

Does anyone have an idea of how I can adapt the code accordingly?

How to solve:

From other parts of your code (e.g. where you create <a href text and so on), it looks like your email is intended to be a HTML-formatted email.

Assuming that’s correct, then it’s expected that newline (\n or \r\n) characters won’t work – HTML does not recognise them as formatting characters. It’s likely you’d actually be seeing them as part of the finished output, in fact.

In HTML, you can specify a line break with the <br/> tag instead. Run this demo to see it in action:

Line 1<br/>Line2

Leave a Reply

Your email address will not be published. Required fields are marked *