Skip to content

Instantly share code, notes, and snippets.

@MaximeCulea
Last active April 15, 2019 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaximeCulea/8f9f59840fdd91949e42ee33be78e634 to your computer and use it in GitHub Desktop.
Save MaximeCulea/8f9f59840fdd91949e42ee33be78e634 to your computer and use it in GitHub Desktop.
Escape textarea by preserving returning lines.
<?php
/**
* Escape by preserving the newlines : <br />
* By applying wp_kses on a textarea
*
* @param $string
*
* @return mixed
* @author Maxime CULEA
*/
function mc_esc_textarea( $string ) {
// Replace <br> <br/> <br > <br /> with %newline%
$_string = preg_replace( '/(<br\s*\/?>\s*)/mi', "%newline%", $string );
// Sanitize the textearea
$_string = wp_kses( $_string, '' );
// Put back the <br />
$_string = str_replace( '%newline%', '<br />', $_string );
// Delete last empty useless characters
return trim( $_string );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment