468,780 Members | 2,309 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

Func to Turn Relative URLs into Abs URLs

tom
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($html,$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.

Dec 12 '05 #1
10 2946
i desperately need a function that will transform relative URLs to
absolute URLs in the SRC part of <img> tags.


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

---
Steve

Dec 12 '05 #2
tom
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.

Dec 12 '05 #3
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).


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

Dec 12 '05 #4
tom
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

Dec 12 '05 #5
*** tom escribió/wrote (12 Dec 2005 08:11:29 -0800):
(a)detect the relative URLs with a REgEx
I'd say a simple strpos() should do this.

(b)add the base href to the start of these relative URLs


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
--
Dec 12 '05 #6
tom
Hi Alvara,

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

http://www.7inchpunk.com/?feed=rss2

(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...

Dec 13 '05 #7
tom
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:
http://sourceforge.net/projects/snoopy/

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($links,$URI)
{

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

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

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

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

$expandedLinks = preg_replace($search,$replace,$links);

return $expandedLinks;
}

Dec 13 '05 #8
tom wrote:
i desperately need a function that will transform relative URLs to
absolute URLs in the SRC part of <img> tags.


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:

http://www.w3.org/2004/04/uri-rel-test.html

--
Jock
Dec 14 '05 #9
tom wrote:
hi group,

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

<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/

Dec 15 '05 #10
tom
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!

Dec 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Joe Cybernet | last post: by
24 posts views Thread by sinister | last post: by
1 post views Thread by MattC | last post: by
reply views Thread by Paul Hatcher | last post: by
9 posts views Thread by Paul Hatcher | last post: by
reply views Thread by Flip | last post: by
15 posts views Thread by Lars Eighner | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.