<?php
function sanitiseFilename($input) {
//
// See #3271
// Code from http://stackoverflow.com/a/2021729/3929494
//
// Remove anything which isn't a word, whitespace, number
// or any of the following characters -_~,;:[]().
// If you don't need to handle multi-byte characters
// you can use preg_replace rather than mb_ereg_replace
// Thanks @Łukasz Rysiak!
$output = mb_ereg_replace("([^\w\s\d\-_~,;:\[\]\(\).])", '', $input);
// Remove any runs of periods (thanks falstro!)
$output = mb_ereg_replace("([\.]{2,})", '', $output);
// Remove any runs of spaces, e.g. " / " becomes " " which is ugly
$output = mb_ereg_replace(" ", " ", $output);
return $output;
}
echo sanitiseFilename("BBC/TVL") . PHP_EOL;
echo sanitiseFilename("This%%% is an an$57noy£ing string/ to / use") . PHP_EOL;
?>