Skip to content

Instantly share code, notes, and snippets.

@Daniil-Solovyev
Created June 30, 2021 09:30
Show Gist options
  • Save Daniil-Solovyev/bb873ee04bf0e14f07320997f4bf7b85 to your computer and use it in GitHub Desktop.
Save Daniil-Solovyev/bb873ee04bf0e14f07320997f4bf7b85 to your computer and use it in GitHub Desktop.
Удаление лишних пробелов
<?php
$input = ' On my home world';
/**
* @param string $input
* @param $separator
* @return string
*/
function emptySpaces(string $input, $separator): string
{
for ($i = 0; $i <= strlen($input); $i++) {
if ($input[$i] === $separator) {
while ($input[$i + 1] === $separator) {
$input = substr_replace($input, '', $i, 1);;
}
}
}
return $input;
}
echo emptySpaces($input, ' ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment