Skip to content

Commit

Permalink
Introduce wpcf7_strip_whitespaces()
Browse files Browse the repository at this point in the history
  • Loading branch information
takayukister committed Jul 20, 2024
1 parent ff6e661 commit 90466ca
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,41 @@ function wpcf7_format_atts( $atts ) {

return trim( $output );
}


/**
* Strips surrounding whitespaces.
*
* @link https://contactform7.com/2024/07/13/consistent-handling-policy-of-surrounding-whitespaces/
*
* @param string $input Input text.
* @param string|array $options Options.
* @return string Output text.
*/
function wpcf7_strip_whitespaces( $input, $options = '' ) {
$options = wp_parse_args( $options, array(
'strip_leading' => true,
'strip_trailing' => true,
) );

// https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt
$whitespaces = '\x09-\x0D\x20\x85\xA0\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}';

if ( $options['strip_leading'] ) {
$input = preg_replace(
sprintf( '/^[%s]+/u', $whitespaces ),
'',
$input
);
}

if ( $options['strip_trailing'] ) {
$input = preg_replace(
sprintf( '/[%s]+$/u', $whitespaces ),
'',
$input
);
}

return $input;
}

0 comments on commit 90466ca

Please sign in to comment.