473,797 Members | 3,199 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Dec 12 '05 #1
10 3163
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:encode d> 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($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;
}

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9151
by: Joe Cybernet | last post by:
Is there any function for combining an absolute and a relative URL to result in an absolute URL? Like if I have http://www.domain.com and "../images/1.jpeg" it will evaluate to http://www.domain.com/images/1.jpeg. I know WinInet for windows has a function that does this called InternetCombineUrl, I just need the same function in PHP
2
2531
by: twells | last post by:
Anyone know of a site that gives all of the syntax of relative addressing for URLS? for example: /, ./, ../, etc. Thanks!
24
4499
by: sinister | last post by:
After doing a websearch, it appears that it's OK to omit the "http:" to form a relative URL. Are there any pitfalls to this? For example, if there is a page http://www.domain1.com/page1.html with a link to http://www.domain2.com/page2.html you can abbreviate the second link as //www.domain2.com/page2.html
1
1571
by: MattC | last post by:
I have the following in the Page_Error method of my base class for my aspx pages. public void Page_Error(Object sender, EventArgs e) { Server.Transfer(Request.ApplicationPath + "/Error.aspx"); } This works fine, however Error.aspx has some images on it that are document relative, but if a page causes an error that is not in the same path as
0
1302
by: Paul Hatcher | last post by:
I'm using a url rewriting mechanism to make my site's urls more search-engine/user-friendly, so we have http://www.mysite.com/links/1023.apx which is actually internally mapped to http://www.mysite.com/links.aspx?link=1023. This works fine and makes the site look a lot cleaner. The problem I'm having is with controls on the target page. I render all the urls as root relative so that I'm independent of whether I'm a root web or an...
9
1811
by: Paul Hatcher | last post by:
Sorry to post this again - I didn't use my MSDN alias previously... I'm using a url rewriting mechanism to make my site's urls more search-engine/user-friendly, so we have http://www.mysite.com/links/1023.apx which is actually internally mapped to http://www.mysite.com/links.aspx?link=1023. This works fine and makes the site look a lot cleaner. The problem I'm having is with controls on the target page. I render all the urls as root...
0
1118
by: Flip | last post by:
I am retrieving a web page and then taking an inside table to display as an RSS feed. However, there are some relative URLs in there. Of course they work perfectly fine when using IE and navigating to the page. However when I scrape the html out, and load into a temp file, they longer work due to the relative urls. Is there an easy (ie a method to run over the html table or the request) to transform the relative URLs to absolute ones?...
5
2167
shishisu
by: shishisu | last post by:
I figure this might be a fun challenge to some of you out there... I appreciate all the help. Thanks in advance. Here we go... I have 2 web page (on different servers) I am working on. Here, we'll call them Page-A and Page-B. I want to save some work by only having to maintain 1 web page and the changes made to source page will get reflect onto the other one. In this case, Page-A is the source and Page-B should just have a content...
15
6471
by: Lars Eighner | last post by:
Aside from the deaths of a few extra electrons to spell out the whole root relative path, is there any down side? It seems to me that theoretically it shouldn't make any difference, and it would make it much easier to slap modualar blocks of markup into page frameworks, which may change and so forth. And the few extra bytes, which even for a fairly large site would not amount to as many bytes as are in a fairly small low-res image, should...
0
9537
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10246
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.