Skip to content

Instantly share code, notes, and snippets.

@fhferreira
Created April 15, 2024 20:55
Show Gist options
  • Save fhferreira/47b6f90e0e2f51d11caa1b773a8dbd58 to your computer and use it in GitHub Desktop.
Save fhferreira/47b6f90e0e2f51d11caa1b773a8dbd58 to your computer and use it in GitHub Desktop.
replaced duplicated spaces with PHP
<?php
//Replacing multiple spaces with a single space simple single line of php code:
$output = preg_replace('/\s+/', ' ', $input);
//This mean that spaces, tabs or line breaks (one or more) will be replaced by a single space.
//Replace multiple spaces with one space:
$output = preg_replace('/\s+/', ' ', $input);
//Replace one or multiple tabs with one space:
$output = preg_replace('/\t+/', ' ', $input);
//Replace one or multiple new lines with one space:
$output = preg_replace('/\n\r+/', ' ', $input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment