Skip to content

Instantly share code, notes, and snippets.

@garvin
Last active September 10, 2015 17:21
Show Gist options
  • Save garvin/2716673cf7967c0d4319 to your computer and use it in GitHub Desktop.
Save garvin/2716673cf7967c0d4319 to your computer and use it in GitHub Desktop.
Useful string functions
/**
* Useful string functions
*/
/**
* Replace any whitespace character or sequence of whitespace characters into a single space
*
* @param string $str
* @return string $str with any sequence of whitespace collapsed into a single space
*/
function collapse_whitespace($str) {
return preg_replace('/\\s+/', ' ', $str);
}
/**
* Remove or replace known problematic characters
*
* xA0 = newline
* x00 = NULL (which could be interpretted as end of string)
*
* @param string $str
* @param string $replacement What to use as a replacement. Optional. Default is an empty string (i.e., remove).
* @return string $str without known problem characters replaced/removed.
*/
function remove_problem_characters($str, $replacement = '') {
return strtr((string)$str, array("\xa0" => $replacement, "\x00" => $replacement));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment