Func to Turn Relative URLs into Abs URLs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tom

    Func to Turn Relative URLs into Abs URLs

    hi group,

    i desperately need a function that will transform relative URLs to
    absolute URLs in the SRC part of <img> tags.

    ie:

    function makeAbsolute($h tml,$basehref)
    {
    //if regex match = relative URL ==> return img tag with absolute URL
    }

    i've scoured Google, PHP.net, PHPClasses.org, etc.., but have been
    unable to find such a function.

    since i'm an idiot regarding anything concerning RegEx, i've been
    unable to write this function on my own.

    if anyone has such a function, i'd be very glad if you could please
    post it.

  • Steve

    #2
    Re: Func to Turn Relative URLs into Abs URLs

    [color=blue]
    > i desperately need a function that will transform relative URLs to
    > absolute URLs in the SRC part of <img> tags.[/color]

    There are several suggestions in the user notes for this function
    <http://www.php.net/realpath>.

    ---
    Steve

    Comment

    • tom

      #3
      Re: Func to Turn Relative URLs into Abs URLs

      hey thanks a lot for the helpful suggestion, steve !

      however, it looks like the functions on php.net are in regarding to
      replacing links to relative LOCAL files (as opposed to remote, on other
      servers).

      still, this may be a good starting point for me.

      Comment

      • Steve

        #4
        Re: Func to Turn Relative URLs into Abs URLs

        [color=blue]
        > however, it looks like the functions on php.net are in regarding to
        > replacing links to relative LOCAL files (as opposed to remote, on other
        > servers).[/color]

        Well, yes. Your comment implies that you are trying to make corrections
        to user-entered URLs. How can you know what the allowable paths might
        be for an arbitrary remote site? The best you can do is to validate
        that the entered string matches your preferences, but you can't correct
        the URL as you can't walk the remote web directory tree.

        ---
        Steve

        Comment

        • tom

          #5
          Re: Func to Turn Relative URLs into Abs URLs

          i was thinking of using preg_match to search for Relative URLS
          beginning with either a '/' or something other than 'http://',
          'https://' or 'ftp://'.

          then, since i KNOW the Base HREF, adding the Base HREF to the beginning
          of these URLS.

          FYI, as background: i'm working with a PHP app that deals with images
          found in RSS / Atom newsfeeds. stupidly a lot of applications, blogs,
          etc, create newsfeeds containing images with paths relative to the web
          root, so all i'd have to do is

          (a)detect the relative URLs with a REgEx
          (b)add the base href to the start of these relative URLs

          Comment

          • Alvaro G. Vicario

            #6
            Re: Func to Turn Relative URLs into Abs URLs

            *** tom escribió/wrote (12 Dec 2005 08:11:29 -0800):[color=blue]
            > (a)detect the relative URLs with a REgEx[/color]

            I'd say a simple strpos() should do this.

            [color=blue]
            > (b)add the base href to the start of these relative URLs[/color]

            Could you provide some sample data? I guess that if it was something as
            simple as concatenating you would not be asking :)

            --
            -+ Álvaro G. Vicario - Burgos, Spain
            ++ http://bits.demogracia.com es mi sitio para programadores web
            +- http://www.demogracia.com es mi web de humor libre de cloro
            --

            Comment

            • tom

              #7
              Re: Func to Turn Relative URLs into Abs URLs

              Hi Alvara,

              As an example of the data I'm dealing with, take a look at the
              <content:encode d> tags in this newsfeed:



              (BTW, that's a great site there !)

              I'm using the Magpie (http://magpierss.sourceforge.net/) to parse the
              feed. Magpie returns the entire feed elements as array elements. Now I
              need a function that searchs for the SRC= part of the <img> tag in the
              feed <description> , ie: using a RegEx like

              src[^>]*[^/].(?:jpg|bmp|gif )(?:\"|\'|)

              And then tests if this contains a Relative or an Absolute URL.

              If it's relative it should then simply concatenate, adding the Base
              HREF to the start of the relative URL...

              Anyone want to try writing such a function ? I think it'll have
              applications to other stuff besides parsing newsfeeds...

              Comment

              • tom

                #8
                Re: Func to Turn Relative URLs into Abs URLs

                FYI, by accident I just found a function that's pretty much what I was
                looking for.

                It's from the GREAT PHP web browser simulation class 'Snoopy', which
                I've been using for ages now with great results:
                Download Snoopy for free. Snoopy is a PHP class that simulates a web browser. It automates the task of retrieving web page content and posting forms, for example.


                In case this thread is ever helpful to someone else, here's the
                function _expandlinks from Snoopy:

                /*============== =============== =============== =============== ===========*\
                Function: _expandlinks
                Purpose: expand each link into a fully qualified URL
                Input: $links the links to qualify
                $URI the full URI to get the base from
                Output: $expandedLinks the expanded links
                \*============= =============== =============== =============== ============*/

                function _expandlinks($l inks,$URI)
                {

                preg_match("/^[^\?]+/",$URI,$mat ch);

                $match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]);
                $match = preg_replace("|/$|","",$match );
                $match_part = parse_url($matc h);
                $match_root =
                $match_part["scheme"]."://".$match_pa rt["host"];

                $search = array( "|^http://".preg_quote($t his->host)."|i",
                "|^(\/)|i",
                "|^(?!http://)(?!mailto:)|i" ,
                "|/\./|",
                "|/[^\/]+/\.\./|"
                );

                $replace = array( "",
                $match_root."/",
                $match."/",
                "/",
                "/"
                );

                $expandedLinks = preg_replace($s earch,$replace, $links);

                return $expandedLinks;
                }

                Comment

                • John Dunlop

                  #9
                  Re: Func to Turn Relative URLs into Abs URLs

                  tom wrote:
                  [color=blue]
                  > i desperately need a function that will transform relative URLs to
                  > absolute URLs in the SRC part of <img> tags.[/color]

                  RFC3986 describes the definitive algorithm for resolving relative
                  references, given a base URI, and how to, in general, establish that base
                  URI.

                  There's nothing to stop you from writing your own algorithm or making
                  use of someone else's, so long as it measures up. If you do decide to
                  write one, here's some examples to test with:



                  --
                  Jock

                  Comment

                  • R. Rajesh Jeba Anbiah

                    #10
                    Re: Func to Turn Relative URLs into Abs URLs

                    tom wrote:[color=blue]
                    > hi group,
                    >
                    > i desperately need a function that will transform relative URLs to
                    > absolute URLs in the SRC part of <img> tags.[/color]
                    <snip>

                    As you mentioned, you can hack Snoopy source or look at here
                    <http://www.php-faq.de/q/q-regexp-links-absolut.html>

                    --
                    <?php echo 'Just another PHP saint'; ?>
                    Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

                    Comment

                    • tom

                      #11
                      Re: Func to Turn Relative URLs into Abs URLs

                      hey Rajesh,

                      i just read your post - that's a great link! thanks very much for
                      posting that.

                      it's exactly the function i was looking for, and was probably written
                      by a much better programmer than myself, saving me time, testing and
                      probably a whole lot of frustration.

                      Thanks again!

                      Comment

                      Working...