$string = \"My text has so much whitespace
Plenty of spaces and tabs\";
echo preg_replace(\"/\\s\\s+/\", \" \", $string);
Not sure if this will be useful nor am I absolutely positive it works like it should but it seems to be working for me.
A function that clears multiple spaces and anything else you want or don't want and produces either a single line string or a multi-line string (dependent on passed arguments/options). Can also remove or keep characters for other languages and convert newline tabs to spaces.
/** ¯\_(ツ)_/¯ Hope it's useful to someone. **/
// If $multiLine is null this removes spaces too. '[:emoji:]' with $l = true allows only known emoji.
// '[:print:]' with $l = true allows all utf8 printable chars (including emoji).
// **** TODO: If a unicode emoji or language char is used in $options while $l = false; we get an odd � symbol replacement for any non-matching char. $options char seems to get through, regardless of $l = false ? (bug (?)interesting)
function alphaNumericMagic($value, $options = '', $l = false, $multiLine = false, $tabSpaces = " ") {
$utf8Emojis = '';
$patterns = [];
$replacements = [];
if ($l && preg_match("~(\[\:emoji\:\])~", $options)) {
$utf8Emojis = [
'\x{1F600}-\x{1F64F}', /* Emoticons */
'\x{1F9D0}-\x{1F9E6}',
'\x{1F300}-\x{1F5FF}', /* Misc Characters */ // \x{1F9D0}-\x{1F9E6}
'\x{1F680}-\x{1F6FF}', /* Transport and Map */
'\x{1F1E0}-\x{1F1FF}' /* Flags (iOS) */
];
$utf8Emojis = implode('', $utf8Emojis);
}
$options = str_replace("[:emoji:]", $utf8Emojis, $options);
if (!preg_match("~(\[\:graph\:\]|\[\:print\:\]|\[\:punct\:\]|\\\-)~", $options)) {
$value = str_replace("-", ' ', $value);
}
if ($l) {
$l = 'u';
$options = $options . '\p{L}\p{N}\p{Pd}';
} else { $l = ''; }
if (preg_match("~(\[\:print\:\])~", $options)) {
$patterns[] = "/[ ]+/m";
$replacements[] = " ";
}
if ($multiLine) {
$patterns[] = "/(?
Example usage:
echo header('Content-Type: text/html; charset=utf-8', true);
$string = "fjl!sj\nfl _ sfjs-lkjf\r\n\tskj 婦女與環境健康 fsl \tklkj\thl jhj ⚧