0

What is the best way to decompose the following string:

$str = '/input-180x129.png'

Into the following:

$array = array(
    'name' => 'input',
    'width' => 180,
    'height' => 129,
    'format' => 'png',
);
Dragos Rizescu
  • 3,380
  • 5
  • 31
  • 42

2 Answers2

6

I would just use preg_split to split the string into several variables and put them into an array, if you must.

$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
);

After Esailija's great comment I've made new code that should work better!

We simply get all matches from a preg_match and do pretty much the same we did on previous code.

$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
);
Tim S.
  • 13,597
  • 7
  • 46
  • 72
  • 2
    What if the name has `x`? It would still be unambiguous as long as name cannot have `-` but this would fail I think. – Esailija Apr 10 '13 at 11:05
  • Haven't thought about that! Will craft new code... -beep beep- – Tim S. Apr 10 '13 at 11:07
  • Thanks for the fast answer, but what if my $str is '/directory/subdirectory/anothersubdirectory/input-180x129.png'. How would you get only the 'input-180x129.png'? – Dragos Rizescu Apr 10 '13 at 11:28
  • Also, if you don't mind, can you link some documentation about the // regex to match filename? I want to learn more about how to write preg_match patters like that that, I can't seem to find anything exacly on google. :( – Dragos Rizescu Apr 10 '13 at 11:32
  • 1
    @DragosRizescu I've changed my code so it uses [`pathinfo()`](http://php.net/manual/en/function.pathinfo.php). That function splits a string into an associate array containing the path, filename, etc. – Tim S. Apr 10 '13 at 11:34
  • 1
    @DragosRizescu You can look it all up on [PHP's PCRE documentation](http://www.php.net/manual/en/reference.pcre.pattern.syntax.php) – Tim S. Apr 10 '13 at 11:36
  • Thanks again, if I ever get the chance to meet you in real life, you've got a beer from me :) – Dragos Rizescu Apr 10 '13 at 11:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27949/discussion-between-dragos-rizescu-and-tim-s) – Dragos Rizescu Apr 10 '13 at 13:53
0

This might be a slow & stupid solution, but it is easier to read:

$str = substr($str, 1);       //  /input-180x129.png => input-180x129.png
$tokens = explode('-', $str);
$array = array();
$array['name'] = $tokens[0];
$tokens2 = explode('.', $tokens[1]);
$array['format'] = $tokens2[1];
$tokens3 = explode('x', $tokens2[0]);
$array['width'] = $tokens3[0];
$array['height'] = $tokens3[1];
print_r($array);

// will result:
$array = array(
    'name' => 'input',
    'width' => 180,
    'height' => 129,
    'format' => 'png',
);
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • 2
    It is not easier to read if you know just the basics of regular expressions. And if one doesn't know, they should learn them and not write mountains of code to express something that can be succinctly expressed by a regex. – Esailija Apr 10 '13 at 11:11