What best practices should be followed when integrating JavaScript code for copying text with line breaks to the clipboard in PHP?

When integrating JavaScript code for copying text with line breaks to the clipboard in PHP, it is important to properly encode the text to ensure that line breaks are preserved. This can be achieved by using the `json_encode` function in PHP to encode the text before passing it to the JavaScript code. Additionally, the JavaScript code should handle the decoding of the encoded text to properly display the line breaks.

<?php

$text = "This is a text with line breaks.\nLine 1\nLine 2";

?>

<script>
    var text = <?php echo json_encode($text); ?>;
    
    function copyTextToClipboard(text) {
        var textArea = document.createElement("textarea");
        textArea.value = text;
        document.body.appendChild(textArea);
        textArea.select();
        document.execCommand("copy");
        document.body.removeChild(textArea);
    }

    copyTextToClipboard(text);
</script>