Regular Expression
For a regular expression to be used, it must be able to extract both $width
and $height
. The following, for example, does not provide sufficient simplifications over the original code:
<?php
$test = array(
"http://localhost/images/filename-200.jpg",
"http://localhost/images/filename-200x.jpg",
"http://localhost/images/filename-x200.jpg",
"http://localhost/images/filename-200x200.jpg"
);
foreach( $test as $t ) {
echo "Test:\n";
$path = pathinfo( $t );
print_r( $path );
//preg_match( "!.*-(\d+)?x(\d+)?$!", $path["filename"], $matches );
preg_match( '!^([A-Za-z0-9_]+)-(\d+)?x(\d+)?$!', $path["filename"], $matches );
echo "Matches:\n";
print_r( $matches );
// Depending on what gets matched, the array indexes might vary.
// $width = $matches[...];
// $height = $matches[...];
}