wp_strip_all_tags( string $string, bool $remove_breaks = false )
Enlève proprement toutes les balises HTML incluant script et style.
Paramètres
$string(string)requisChaîne pouvant contenir des balises HTML.
$remove_breaks(bool)optionnelPour enlever les retours à la ligne et les les espaces blancs.
Valeur par défaut : false
Description / Informations supplémentaires
wp_strip_all_tags() diffère de la fonction php strip_tags() car elle enlève en plus le contenu des balises <script> et <style>.
Exemple : strip_tags( '<script>something</script>' ) retournera 'something' alors que wp_strip_all_tags() retournera ''.
Retourne
(string) La chaîne nettoyée.
Déclaration et structure de la fonction wp_strip_all_tags()
wp_strip_all_tags() est déclarée dans le fichier wp-includes/formatting.php à la ligne 5514 :
function wp_strip_all_tags( $text, $remove_breaks = false ) {
if ( is_null( $text ) ) {
return '';
}
if ( ! is_scalar( $text ) ) {
/*
* To maintain consistency with pre-PHP 8 error levels,
* wp_trigger_error() is used to trigger an E_USER_WARNING,
* rather than _doing_it_wrong(), which triggers an E_USER_NOTICE.
*/
wp_trigger_error(
'',
sprintf(
/* translators: 1: The function name, 2: The argument number, 3: The argument name, 4: The expected type, 5: The provided type. */
__( 'Warning: %1$s expects parameter %2$s (%3$s) to be a %4$s, %5$s given.' ),
__FUNCTION__,
'#1',
'$text',
'string',
gettype( $text )
),
E_USER_WARNING
);
return '';
}
$text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $text );
$text = strip_tags( $text );
if ( $remove_breaks ) {
$text = preg_replace( '/[\r\n\t ]+/', ' ', $text );
}
return trim( $text );
}
Où est utilisée la fonction wp_strip_all_tags()
dans le CMS WordPress
Exemple
$html = '<strong>I am not strong</strong>';
var_dump($html);
//output '<strong>I am not strong</strong>'
var_dump(wp_strip_all_tags($html));
//output 'I am not strong'
Sources
Codex WordPress : wp_strip_all_tags()
Autres fonctions dans le même fichier : wp-includes/formatting.php