What considerations should be made when handling HTML-encoded data while removing line breaks at the end of strings in PHP?

When handling HTML-encoded data in PHP and removing line breaks at the end of strings, it's important to consider that line breaks can be represented as <br>, <br/>, or <br /> in HTML. To remove these line breaks, you can use the rtrim() function in PHP along with the str_replace() function to remove all occurrences of <br>, <br/>, and <br /> at the end of the string.

// Sample HTML-encoded string with line breaks at the end
$htmlEncodedString = &quot;This is a sample string with line breaks&lt;br&gt;&lt;br&gt;&quot;;

// Remove line breaks at the end of the string
$cleanedString = rtrim(str_replace([&#039;&lt;br&gt;&#039;, &#039;&lt;br/&gt;&#039;, &#039;&lt;br /&gt;&#039;], &#039;&#039;, $htmlEncodedString));

echo $cleanedString;