Je voudrais simplement utiliser preg_split pour diviser the string into several variables et put them into an array, si vous devez.
$str = 'path/to/input-180x129.png';
// get info of a path
$pathinfo = pathinfo($str);
$filename = $pathinfo['basename'];
// regex to split on "-", "x" or "."
$format = '/[\-x\.]/';
// put them into variables
list($name, $width, $height, $format) = preg_split($format, $filename);
// put them into an array, if you must
$array = array(
'name' => $name,
'width' => $width,
'height' => $height,
'format' => $format
);
Après le grand commentaire de Esailija J'ai fait nouveau code qui devrait fonctionner mieux!
Nous obtenons simplement tous les résultats d'un preg_match
et faisons à peu près la même chose que sur le code précédent.
$str = 'path/to/input-180x129.png';
// get info of a path
$pathinfo = pathinfo($str);
$filename = $pathinfo['basename'];
// regex to match filename
$format = '/(.+?)-([0-9]+)x([0-9]+)\.([a-z]+)/';
// find matches
preg_match($format, $filename, $matches);
// list array to variables
list(, $name, $width, $height, $format) = $matches;
// ^that's on purpose! the first match is the filename entirely
// put into the array
$array = array(
'name' => $name,
'width' => $width,
'height' => $height,
'format' => $format
);
Est-ce que le résultat doivent être un tableau associatif? – BenM
que diriez-vous de 'explode()'? – Raptor
Quel est votre code actuel? Où êtes-vous coincé? – Jocelyn