File name sanitizer
This is a way to sanitize a string to be used as file name or URL path etc. by removing unwanted characters or replacing them with a hyphen or similar.
It takes a string like:
Daily SimpliVity Backup 12/16/2018 02:00:215
and converts it into:
daily-simplivity-backup-12-16-2018-02-00-25
<?php
$file_name = preg_replace( '/[^a-z0-9]+/', '-', strtolower( $url ) );
?>
Source: https://stackoverflow.com/a/42908256/1215633
An example of a function
<?php
/**
* Create package URL based on name and id of segment, matrix, area and package
* Sub matrix and sub area gets precedence
* @param type $package
* @return type
*/
public function createPackageUrl($package){
$pattern = array('/å/','/ä/','/ö/','/Å/','/Ä/','/Ö/','/æ/','/ø/','/å/','/Æ/','/Ø/','/Å/','/ü/', '/[^a-z0-9]+/');
$replace = array('a','a','o','a','a','o','ae','o','a','ae','o','a','u', '-');
$path = array();
if(!empty(trim($package['segment_title']))){
$path[] = preg_replace( $pattern, $replace, strtolower(trim($package['segment_title'])))."_".$package['ixSegment'];
}else{
$path[] = "segment_0";
}
if(!empty(trim($package['matrix_sub_title']))){
$path[] = preg_replace( $pattern, $replace, strtolower(trim($package['matrix_sub_title'])))."_".$package['ixSubMatrix'];
}else{
if(!empty(trim($package['matrix_title']))){
$path[] = preg_replace( $pattern, $replace, strtolower(trim($package['matrix_title'])))."_".$package['ixMatrix'];
}else{
$path[] = "matrix_0";
}
}
if(!empty(trim($package['area_sub_title']))){
$path[] = preg_replace( $pattern, $replace, strtolower(trim($package['area_sub_title'])))."_".$package['ixSubArea'];
}else{
if(!empty(trim($package['area_title']))){
$path[] = preg_replace( $pattern, $replace, strtolower(trim($package['area_title'])))."_".$package['ixArea'];
}else{
$path[] = "area_0";
}
}
$path[] = preg_replace( $pattern, $replace, strtolower(trim($package['package_title'])))."_".$package['id'];
return "/".implode("/", $path);
}
// /[SEGMENT]_9/[SUB-MATRIX|MATRIX]_1/[SUB-AREA|AREA]_1/[PACKAGE-TITLE]_[ixPackage]
// [package_url] => /humanbiologi_4/isotopanalyser_39/isotoper_20/isotopanalyser-humanbiologi_5740
/* $package
[id] => 5740
[matrix_title] => Humanbiologi
[area_title] => Isotoper
[matrix_sub_title] => Isotopanalyser
[area_sub_title] =>
[segment_title] => Humanbiologi
[ixArea] => 5
[ixMatrix] => 35
[ixSegment] => 2
[ixSubMatrix] => 18
[ixSubArea] => 0
*/
//Tests
// $package['segment_title'] = " ";
// unset($package['segment_title']);
// $package['matrix_sub_title'] = " ";
// $package['matrix_title'] = "-";
// $package['package_title'] = "one-two--three---underline_connection";
?>
Knowledge keywords: