'Removing HTML Comments from Wordpress Blocks in JSON

I'm trying to make a custom API in Wordpress via register_rest_route() and some CPTs (with CPT-UI Plugin).

All ok but I'm not managing to clean the html comments [created by gutenberg's blocks] from the body text (the_content).

The best solution so far was to put sanitize_text_field( get_the_content() ).

A second solution was to put Show in Rest = false in the CPT Plugin options, which sounds a step back but it removes them.

I also tried wp_strip_all_tags() but some characters were decoded.

But I'm sure there is a better, "official", solid and future proof solution for this..?

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source