상대경로에서 절대경로로 변환하는 문제

acidd15의 이미지

음...html 페이지를 긁어 오는데 그 페이지 소스에서 이미지 및 모든 링크의 주소들을 뽑아 옵니다..

런데 이것들이 절대경로보단 상대경로가 많습니다..이것들이 형식이 일정하지 않고 마구 뒤죽박죽되어 경로가 입력된것들도 감안한다면...

이런 상대 경로들을 절대 경로로 완벽하게 바꿔본다고 했을때..과연 어떤 방식으로 해야 될까요?

p.s 문제의 요지는 상대경로를 어떻게 절대경로로 변환하는가 이며 간단한 소스까지 올려주신다면야 같이 고민해 보도록 하겠습니다.

익명 사용자의 이미지

어떤 방식으로 하긴 모... 좋은 방식으로 하믄 되지...
혼자 고민 많이 하쇼... 나는 하기 싫소.
머 이따위 질문이 다 있남...

익명 사용자의 이미지

여기에도..초딩이..

익명 사용자의 이미지

초딩...나보고 하신 말씀이신가...쩝...
지금 내가 쓴 거 보니까 초딩틱하긴 하네...

질문 올리신 분 태도가 좀 기분 나빠서 초딩틱한 대답을 했네요.

질문하는 사람이 '간단한 소스 올려 주면 내가 같이 고민해 주께...' 이게 뭐냐고요. 선생님이 학생들한테 퀴즈 내는 것 같은 분위기...

별 것도 아닌 거 가지고 뭐 대단한 수수께끼 내는 듯도 하고...

익명 사용자의 이미지

대충 다음과 같은 식으로 하면 되지 않을까요?
php 스타일의 pseudo code입니다.

$curPath = ...; 현재 경로. e.g)dir1/dir2/dir3
$relPath = ...; 상대 경로 e.g)../dir4/file.html

$curPathList = explode("/", $curPath);
$relPathList = explode("/", $curPath);
$tmpList = array_merge($curPathList, $relPathList);

$retList = array();
foreach ( $tmpList as $t )
{
    if ( $t == '..' ) { array_pop($retList); }
    if ( $t != '.' ) { array_push($retList, $t); }
}

$path = implode("/", $retList);

ps. 위에 초딩, 댁이야말로 잘난척만 하고 있지 말고 뭔가 도움되는 얘길 해보지 그래? 할줄아는게 초딩짓밖에 없는가?

익명 사용자의 이미지

정규표현식을 이용한다면 이렇게도...

<?php

$curPath = "http://localhost/dir1/dir2/index.html";
$relPath = "../dir3/./dir4/dir5/../pic1.jpg";

$tmp = preg_replace("/\\/[^\\/]*$/", "/", $curPath) . $relPath;

$tmp = preg_replace("/\\/\\.\\//", "/", $tmp);

$c = preg_match_all("/\\.\\./", $tmp, $matches);
for ( $i = 0; $i < $c; ++$i )
{
    $tmp = preg_replace("/\\/[^\\/]+\\/\\.\\.\\//", "/", $tmp, 1);
}

echo "curPath : $curPath\n";
echo "relPath : $relPath\n";
echo "$tmp\n";

?>

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.