Showing posts with label Remove special characters in string. Show all posts
Showing posts with label Remove special characters in string. Show all posts

Friday, October 19, 2012

Remove special characters from file name

       If you use special characters filename

            $f_name = "filename"; // given filename

            $f_name = trim($f_name,".");  // trim the first or last character "."

            $afilename=explode(".",$f_name);  // explode filename using separator  "."

            $f_ext = end($afilename);  // end keyword used to return the array end of value

            $f_name = reset($afilename); //  reset keyword used to return the array start of value

            $filename = $f_name.".".$f_ext;

            $str    = $filename;

            $sp_chr = array("[,]","[']","[=]","[;]");

            $str = preg_replace($sp_chr,"-",$str);   // using preg replace to replace the . , = ;

            $str = preg_replace("/&/","and",$str);  // replace & to and





           // below steps are replace the . or * or ? or / <  > change to -.

            $str = str_replace(".","-",$str); 
            $str = str_replace(":","-",$str);
            $str = str_replace("*","-",$str);
            $str = str_replace("/","-",$str);
            $str = str_replace("<","-",$str);
            $str = str_replace(">","-",$str);
            $str = str_replace("\"","-",$str);

            $filename = $str;