How to stop xss payload?

<script>alert(‘XSS’)< /script>

these gets gets stored in database. how to deal with these?

Do a sanatize the content before output

say provided we want to clean the data prior saving, where should i start tinkering? a little headstart would be helpful.