Skip to content

Commit 403e6a8

Browse files
committed
fixed url path parsing. related #119
1 parent 2e0e7ce commit 403e6a8

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/Url.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -519,31 +519,30 @@ private function setPath($path)
519519
return;
520520
}
521521

522-
$parts = pathinfo($path);
522+
$file = substr(strrchr($path, '/'), 1);
523523

524-
$this->info['path'] = [];
525-
526-
if (isset($parts['dirname'])) {
527-
foreach (explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $parts['dirname'])) as $dir) {
528-
$dir = trim(urldecode($dir));
524+
if ($file !== '') {
525+
$path = substr($path, 0, -strlen($file));
526+
}
529527

530-
if ($dir !== '') {
531-
$this->info['path'][] = $dir;
532-
}
528+
if ($file) {
529+
if (preg_match('/(.*)\.([\w]+)$/', $file, $match)) {
530+
$this->info['file'] = $match[1];
531+
$this->info['extension'] = $match[2];
532+
} else {
533+
$this->info['file'] = $file;
534+
$this->info['extension'] = null;
533535
}
534536
}
535537

536-
$this->info['file'] = isset($parts['filename']) ? $parts['filename'] : null;
537-
$this->info['extension'] = isset($parts['extension']) ? $parts['extension'] : null;
538-
$this->info['content'] = null;
539-
540-
// bugfix /wiki/Supernatural_(U.S._TV_series) is parsed as:
541-
// path: /wiki/
542-
// file: Supernatural_(U.S
543-
// extension: _TV_series)
544-
if (!empty($this->info['extension']) && !preg_match('/^\w+$/', $this->info['extension'])) {
545-
$this->info['file'] .= '.'.$this->info['extension'];
546-
$this->info['extension'] = null;
538+
$this->info['path'] = [];
539+
540+
foreach (explode('/', $path) as $dir) {
541+
$dir = trim(urldecode($dir));
542+
543+
if ($dir !== '') {
544+
$this->info['path'][] = $dir;
545+
}
547546
}
548547
}
549548
}

tests/UrlTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ public function testParser()
88
'http://vimeo.com//69912181?' => 'http://vimeo.com/69912181',
99
'http://vimeo.com//69912181' => 'http://vimeo.com/69912181',
1010
'http://vimeo.com/69912181' => 'http://vimeo.com/69912181',
11+
'http://vimeo.com/69912181/' => 'http://vimeo.com/69912181/',
1112
'https://www.tumblr.com/oembed/1.0' => 'https://www.tumblr.com/oembed/1.0',
13+
'https://www.tumblr.com/oembed/1.0/' => 'https://www.tumblr.com/oembed/1.0/',
14+
'https://www.tumblr.com/oembed//1.0//' => 'https://www.tumblr.com/oembed/1.0/',
15+
'https://animoto.com/oembeds/create.xml?automated=true&options=start_hq' => 'https://animoto.com/oembeds/create.xml?automated=true&options=start_hq',
16+
'http://static2.politico.com/dims4/default/28fb355/2147483647/resize/1160x%3E/quality/90/?url=http%3A%2F%2Fs3-origin-images.politico.com%2F2013%2F12%2F18%2F131218_george_w_bush_barack_obama_ap_60' => 'http://static2.politico.com/dims4/default/28fb355/2147483647/resize/1160x%3E/quality/90/?url=http%3A%2F%2Fs3-origin-images.politico.com%2F2013%2F12%2F18%2F131218_george_w_bush_barack_obama_ap_60'
1217
];
1318

1419
foreach ($urls as $url => $expected_url) {

0 commit comments

Comments
 (0)