fork download
  1. <?php
  2. // Control de rutas (URL) en PHP para ejecutar peticiones API
  3. // https://es.stackoverflow.com/q/119958/127
  4.  
  5.  
  6. $url = 'http://w...content-available-to-author-only...e.com/padres/col/8';
  7. $regex = '~^https?://[^/]+/(?P<modulo>[-\p{L}]+)(?:/(?P<coleccion>[-\p{L}]+))?(?:/(?P<id>\d+))?/?$~i';
  8.  
  9. if (preg_match( $regex, $url, $matches)) {
  10. $modulo = $matches['modulo'];
  11. $coleccion = isset($matches['coleccion']) ? $matches['coleccion'] : '';
  12. $id = isset($matches['id']) ? $matches['id'] : '';
  13.  
  14. echo "Modulo:\t\t'$modulo'\nColección:\t'$coleccion'\nId:\t\t\t'$id'";
  15. echo "\n\n\$matches = ";
  16. var_export($matches);
  17. } else {
  18. // URL inválida
  19. }
Success #stdin #stdout 0.02s 23748KB
stdin
Standard input is empty
stdout
Modulo:		'padres'
Colección:	'col'
Id:			'8'

$matches = array (
  0 => 'http://w...content-available-to-author-only...e.com/padres/col/8',
  'modulo' => 'padres',
  1 => 'padres',
  'coleccion' => 'col',
  2 => 'col',
  'id' => '8',
  3 => '8',
)