473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regex question...?

How can I easily search a web page for relative paths and prepend the FQDN
to them?

For instance, I send a POST request to a server, and get back the
resulting page, and store it in a variable called $page.

When I print $page, it prints correctly, but things like the styelsheet
and images are broken, since they don't reference the site itself.

Ie, $page contains href="/css/stylesheet.css" as part of the code, and I
need to prepedn "http://www.somesite.com to that so that it gives the full
path and things aren't broken.

Thanks for any and all assistance.

bc90021
Jul 17 '05 #1
4 1593
bc90021 wrote:
How can I easily search a web page for relative paths and prepend the FQDN
to them?

For instance, I send a POST request to a server, and get back the
resulting page, and store it in a variable called $page.

When I print $page, it prints correctly, but things like the styelsheet
and images are broken, since they don't reference the site itself.

Ie, $page contains href="/css/stylesheet.css" as part of the code, and I
need to prepedn "http://www.somesite.com to that so that it gives the full
path and things aren't broken.

Thanks for any and all assistance.

bc90021


Hello,

if you search only URIs delimited by " this would do it:
$newstring = preg_replace('/(\/[^"]*)/', $server."\$1", $yourtext);
if the URI can also be separated by a blank choose this:
$newstring = preg_replace('/(\/[^ "]*)( |")/', $server."\$1\$2", $yourtext);

Regards,
David
Jul 17 '05 #2
bc90021 wrote:

How can I easily search a web page for relative paths and prepend the FQDN
to them?

For instance, I send a POST request to a server, and get back the
resulting page, and store it in a variable called $page.

When I print $page, it prints correctly, but things like the styelsheet
and images are broken, since they don't reference the site itself.

Ie, $page contains href="/css/stylesheet.css" as part of the code, and I
need to prepedn "http://www.somesite.com to that so that it gives the full
path and things aren't broken.

Stick a <BASE HREF="http://thesiteaddress.com/pathinfo/"> into the <HEAD>er.
Much easier to do and much less work for your server than preging the whole
thing.

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com

I have a spam filter. Please include "PHP" in the
subject line to ensure I'll get your message.
Jul 17 '05 #3
On Thu, 22 Jan 2004 11:31:58 +0100, David Rybach wrote:
bc90021 wrote:

<<SNIP>>
Ie, $page contains href="/css/stylesheet.css" as part of the code, and I
need to prepedn "http://www.somesite.com to that so that it gives the full
path and things aren't broken.

Hello,

if you search only URIs delimited by " this would do it:
$newstring = preg_replace('/(\/[^"]*)/', $server."\$1", $yourtext);
if the URI can also be separated by a blank choose this:
$newstring = preg_replace('/(\/[^ "]*)( |")/', $server."\$1\$2", $yourtext);

Regards,
David


David,

This works *perfectly* given what I said above. What I neglected to mention
(because I didn't think of it at the time) is that things like "text/css"
contain a slash, and this works on those as well.

I've used Shawn's suggestion, as that saves significantly on the
processing power, which makes sense given my environment.

Thanks!

bc90021


Jul 17 '05 #4
On Thu, 22 Jan 2004 10:34:06 -0400, Shawn Wilson wrote:
bc90021 wrote:

<<SNIP>>
Ie, $page contains href="/css/stylesheet.css" as part of the code, and I
need to prepedn "http://www.somesite.com to that so that it gives the full
path and things aren't broken.


Stick a <BASE HREF="http://thesiteaddress.com/pathinfo/"> into the <HEAD>er.
Much easier to do and much less work for your server than preging the whole
thing.

Regards,
Shawn


Shawn,

Thanks, that works like a charm. I replaced "<head>" with "<head><base
href...>" in the page and everything loads perfectly.

bc90021

Jul 17 '05 #5

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

Similar topics

4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.