在表单提交后从textarea中删除多个空格

I am using contact form 7 on my Wordpress website to show an enquiry form to my users.

I recentley modified the form and added a textarea. However the issue I am having is removing the whitespace added by users when they hold down on the "enter" key on the front end. Some users will add hundreds of lines of whitespace which is not desireable.

Ideally when they submit the form I'd like to replace all this whitespace with just one space. So I tried the below:

add_filter('wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 10, 2);
function custom_textarea_validation_filter($result, $tag){
    $tag = new WPCF7_Shortcode($tag);
    $value = $_POST[$tag->name];
    if("your-enquiry" == $tag->name){
        htmlentities($value);
        trim(preg_replace('/\s\s+/', ' ', $value));
    }
    return $result;
}

Unfortunately it is not working... I'm not so concerned about preventing them doing it on the front end as some users may want to seperate out their paragraphs etc... but on server side I'd like to remove all of this whitespace and replace with one blank line

anyone have any suggestions?

You are not assigning any value to $result why you are returning it ? And also your regex is wrong try this :

add_filter('wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 10, 2);
function custom_textarea_validation_filter($result, $tag){
    $tag = new WPCF7_Shortcode($tag);
    $value = $_POST[$tag->name];
    if("your-enquiry" == $tag->name){
        $value  = htmlentities($value);
        $result = trim(preg_replace('/\s{2,}/', ' ', $value));
    }
    return $result;
}

Try this:

add_filter('wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 10, 2);
function custom_textarea_validation_filter($result, $tag){
    $tag = new WPCF7_Shortcode($tag);
    $value = $_POST[$tag->name];
    if("your-enquiry" == $tag->name){
        $value = htmlentities($value);
        $result = trim(preg_replace('/|
/', ' ', $value));
    }
    return $result;
}