HTMLページをパースしてURLを取り出す処理を書いていたのですが、ページ内のリンクなどが全部絶対URLで記述されていれば非常に楽なのですが、現実としてそうでもなく、ページによっては相対パスで書かれていたりして、正規表現で偏にリンクからURLを抜き出すだけではうまくできませんでした。
そこで少しググってみたら
PHPで相対パスから絶対URL(URI)を作成する|PHPプログラムメモ|プログラムメモ
という記事を発見!おぉ、これは便利!
と思って使わせてもらおうと思ったのですが、いくつかテストしてみて、相対パス処理で不備があるなーと思ったところがあったのでちょっと改良させてもらいました。
37~38行目は正直いらない気がしたのですが、 PHP 5.3 のCLIでWindows上でテストした際に、なぜか \/ (アルファベットのVではなく、\ と / ) で出力されたのが気になったので、無駄かもしれないけどあえて記述。
あと $parse の初期化もここまでする必要ないけど、念のためNotice対策を…w
相対パスから絶対URLする関数
-
<?php
-
/**
-
* 相対パスから絶対URLを返します
-
*
-
* @param string $base ベースURL(絶対URL)
-
* @param string $relational_path 相対パス
-
* @return string 相対パスの絶対URL
-
* @link http://blog.anoncom.net/2010/01/08/295.html
-
* @link http://logic.stepserver.jp/data/archives/501.html
-
*/
-
function createUri( $base = '', $relational_path = '' ) {
-
-
'scheme' => null,
-
'user' => null,
-
'pass' => null,
-
'host' => null,
-
'port' => null,
-
'path' => null,
-
'query' => null,
-
'fragment' => null,
-
);
-
-
// 相対パスがURLで指定された場合
-
return $rel_path;
-
// ドキュメントルート指定
-
return $parse['scheme'] . '://' . $parse ['host'] . $relational_path;
-
} else {
-
// 相対パス処理
-
foreach ( $relPath as $relDirName ) {
-
if ($relDirName == '.') {
-
} elseif ($relDirName == '..') {
-
}
-
} else {
-
}
-
}
-
return $parse ['scheme'] . '://' . $parse ['host'] . $path;
-
}
-
}
テストケース
ベースURL
http://example.com/path/to/url
相対パス
- /
- /index.html
- /foo/bar/baz/
- ./foo/bar/baz
- ../../../foo/bar/baz/index.html
- foo/bar/baz.html
- foo/bar/baz/../index.html
テストコード
-
<?php
-
$base = 'http://example.com/path/to/url';
-
-
$pathes[] = '/';
-
$pathes[] = '/index.html';
-
$pathes[] = '/foo/bar/baz/';
-
$pathes[] = './foo/bar/baz';
-
$pathes[] = '../../../foo/bar/baz/index.html';
-
$pathes[] = 'foo/bar/baz.html';
-
$pathes[] = 'foo/bar/baz/../index.html';
-
-
foreach ( $pathes as $path ) {
-
}
結果
こんな感じ
http://example.com/
http://example.com/index.html
http://example.com/foo/bar/baz/
http://example.com/path/to/foo/bar/baz
http://example.com/foo/bar/baz/index.html
http://example.com/path/to/foo/bar/baz.html
http://example.com/path/to/foo/bar/index.html