Skip to content

Instantly share code, notes, and snippets.

@joostvanveen
Created March 3, 2013 17:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save joostvanveen/5077213 to your computer and use it in GitHub Desktop.
Save joostvanveen/5077213 to your computer and use it in GitHub Desktop.
A pre_replace filter that strip out all characters that are NOT letters or numbers or Textile Markup special characters. You can use this to filter user input. If you have any improvements, let me know!
<?php
/**
* Filter input based on a whitelist. This filter strips out all characters that
* are NOT:
* - letters
* - numbers
* - Textile Markup special characters.
*
* Textile markup special characters are:
* _-.*#;:|!"+%{}@
*
* This filter will also pass cyrillic characters, and characters like é and ë.
*
* Typical usage:
* $string = '_ - . * # ; : | ! " + % { } @ abcdefgABCDEFG12345 éüртхцчшщъыэюьЁуфҐ ' . "\nAnd another line!";
* echo textile_sanitize($string);
*
* @param string $string
* @return string The sanitized string
* @author Joost van Veen
*/
function textile_sanitize($string){
$whitelist = '/[^a-zA-Z0-9а-яА-ЯéüртхцчшщъыэюьЁуфҐ \.\*\+\\n|#;:!"%@{} _-]/';
return preg_replace($whitelist, '', $string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment