Skip to main content
deleted 961 characters in body
Source Link
Dave Jarvis
  • 1.9k
  • 1
  • 12
  • 28

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[...];
}

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[...];
}
deleted 2 characters in body
Source Link
Dave Jarvis
  • 1.9k
  • 1
  • 12
  • 28
<?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[...];
}
<?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[...];
}
<?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[...];
}
added 963 characters in body
Source Link
Dave Jarvis
  • 1.9k
  • 1
  • 12
  • 28

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[...];
}

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[...];
}
added 75 characters in body
Source Link
Dave Jarvis
  • 1.9k
  • 1
  • 12
  • 28
Loading
added 161 characters in body
Source Link
Dave Jarvis
  • 1.9k
  • 1
  • 12
  • 28
Loading
Source Link
Dave Jarvis
  • 1.9k
  • 1
  • 12
  • 28
Loading