Clean Text Function

Posted on Mar 21, 2025

by Jimmy

Last updated on September 29, 2025


A function that will strip out any unwanted tags from a string.

PHP
        
            <?php
/**
 * Remove everything from a string except the tags specified below
 */
function cleanText($text) {
    $text = strip_tags($text, '<em>, <br>, <u>, <span>, <strong>, <sup>');
  
    return $text;
}        
    

Place this code in your functions.php file (or import it in via the functions folder).

Handy when you need to you have a title that requires special styling in the backend (Ex: A different coloured word in a title). For example:

PHP
        
            <?php $title = get_sub_field('title'); ?>

<h2><?= cleanText($title); ?></h2>        
    

Back to Snippets