I use code below for extract numbers and file name of strings with problem standardization
30183308__90_.jpeg
30193253-(100).jpg
30193253__100__.jpg
30193253_ _100_ _.jpg
Use this function
public function refactorFileName($filename)
{
$array = preg_split("/[^A-Za-z0-9]/", $filename);
foreach($array as $key => $value) {
if($value == "") {
unset($array[$key]);
}
}
$array = array_values($array);
$standardFilename = $array[0].'.'.$array[2];
$indexFile = $array[1];
return compact("indexFile","standardFilename");
}
$filename = '30193253_ _100_ _.jpg';
extract(refactorFileName($filename));
echo "New File name -> ".$standardFilename.PHP_EOL;
echo "Index for file -> ".$indexFile.PHP_EOL;
This show (correct):
New File name -> 30193253.jpg
Index for file -> 100
I think there're a better code for regex expresion.
EDIT: It's possible better code on preg_split or better code in general for this question?