Href src ve base url'nin daha karmaşık hale gelmeye başlaması durumunda kabul edilen yanıt çözümünün benim için çalışmadığını öğrendim. örneğin
:
baz URL:
http://www.journalofadvertisingresearch.com/ArticleCenter/default.asp?ID=86411&Type=Article
href src: hatalı geri
/ArticleCenter/LeftMenu.asp?Type=Article&FN=&ID=86411&Vol=&No=&Year=&Any=
:
/ArticleCenter/LeftMenu.asp?Type=Article&FN=&ID=86411&Vol=&No=&Year=&Any=
Doğru URL döner aşağıdaki işlevi bulundu. Bunu bir yorumdan aldım: Isaac Z. Schlueter'den
http://php.net/manual/en/function.realpath.php.Yeterince `daki` `< "http://mydomain.com"= temel href>
http://www.journalofadvertisingresearch.com/ArticleCenter/LeftMenu.asp?Type=Article&FN=&ID=86411&Vol=&No=&Year=&Any=
function resolve_href ($base, $href) {
// href="" ==> current url.
if (!$href) {
return $base;
}
// href="http://..." ==> href isn't relative
$rel_parsed = parse_url($href);
if (array_key_exists('scheme', $rel_parsed)) {
return $href;
}
// add an extra character so that, if it ends in a /, we don't lose the last piece.
$base_parsed = parse_url("$base ");
// if it's just server.com and no path, then put a/there.
if (!array_key_exists('path', $base_parsed)) {
$base_parsed = parse_url("$base/ ");
}
// href="/ ==> throw away current path.
if ($href{0} === "/") {
$path = $href;
} else {
$path = dirname($base_parsed['path']) . "/$href";
}
// bla/./bloo ==> bla/bloo
$path = preg_replace('~/\./~', '/', $path);
// resolve /../
// loop through all the parts, popping whenever there's a .., pushing otherwise.
$parts = array();
foreach (
explode('/', preg_replace('~/+~', '/', $path)) as $part
) if ($part === "..") {
array_pop($parts);
} elseif ($part!="") {
$parts[] = $part;
}
return (
(array_key_exists('scheme', $base_parsed)) ?
$base_parsed['scheme'] . '://' . $base_parsed['host'] : ""
) . "/" . implode("/", $parts);
}
Ve' kullanarak olmaz:
Bu doğru döndü? Değilse, o zaman bir regex kullanmanız yeterlidir - aslında 'preg_replace_all' işlevini kullanmanız gerekir. – shadyyx
Cevabınız için teşekkür ederiz. İyi bir öneri, ama sanmıyorum, çünkü çıktı bir XML belgesinde gösterilecek. Sorun regex ile yetersiz olduğum ... – maartenmachiels