nortmas
7/25/2016 - 12:54 PM

Remove everything except letters and numbers

$cleansedstring = preg_replace('#\W#', '', $string);

// or

/**
 * Helper function serving to clear string.
 * @param $str
 * @return mixed|string
 */
function academy_csv_export_clear_str($str) {
  // Remove non breaking spaces
  $str = str_replace(' ', '',$str);
  // Replace all custom tags to HTML tags
  $str = check_markup($str, 'rich');
  // Remove all HTML tags
  $str = strip_tags($str);
  // Remove all tabs and carriage return symbols from string
  $str = preg_replace("/[\t\r]/", "", $str);
  // Replace several newlines in a row with one newline
  $str = preg_replace("/\n+/", "\n", $str);
  // Replace newlines with whitespaces  with only newline
  $str = preg_replace("/\n\s*/", "\n", $str);
  // Remove non breaking spaces and new lines from start and end of the line
  $str = trim($str, "\xC2\xA0\n");
  return $str;
}