Mar
04
2009
Mengubah strange filename ke string
Ini gw dapet dari php.net waktu gw nyari fungsi u/ mengubah character non alphanumeric ke ‘-’ .
Berguna u/ merename filename yg aneh2 atau hal2 lain :
<?</code>
<code> </code>
<code>$string = "d'edy adhie&amp;%sdf\"ddd)(*)@#^$*(&amp;)%sfsd.jpg";
echo "<BR><BR>";
echo "string asli : ".$string."<BR><BR>";
echo "sesudah convert : ".string_to_filename($string);
function string_to_filename($word) {
$pos = strrpos($word,".");
$ext = substr($word,$pos);
$wordToReplace = substr($word,0,$pos);
<span> </span>
<span> </span>// only replace the filename, exclude the extension
$tmp = preg_replace('/\W+|\W+/', '-', $wordToReplace); <span> </span>// remove all non-alphanumeric chars at begin &amp; end of string
$tmp = preg_replace('/\s+/', '-', $tmp); <span> </span>// compress internal whitespace and replace with _
return strtolower(preg_replace('/\W-/', '-', $tmp)).$ext; <span> </span>// remove all non-alphanumeric chars except _ and -
}
</code>
<code>?>
hasil nya :
string asli : d’edy adhie&%sdf”ddd)(*)@#^$*(&)%sfsd.jpg
sesudah convert : d-edy-adhie-sdf-ddd-sfsd.jpg

