Home / PHP Tips Generated By ChatGPT / Encoding HTML Entities in a String in PHP Convert applicable characters to HTML entities with htmlentities() to prevent XSS attacks. Source Code $str = "alert('XSS');"; echo htmlentities($str);