Connecting Tech Pros Worldwide Forums | Help | Site Map

Regex question...?

bc90021
Guest
 
Posts: n/a
#1: Jul 17 '05
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

David Rybach
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Regex question...?


bc90021 wrote:
[color=blue]
> 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[/color]

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
Shawn Wilson
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Regex question...?


bc90021 wrote:[color=blue]
>
> 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.[/color]


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
shawn@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.
bc90021
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Regex question...?


On Thu, 22 Jan 2004 11:31:58 +0100, David Rybach wrote:
[color=blue]
> bc90021 wrote:
>[/color]
<<SNIP>>[color=blue][color=green]
>> 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.
>>[/color]
> 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[/color]

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




bc90021
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Regex question...?


On Thu, 22 Jan 2004 10:34:06 -0400, Shawn Wilson wrote:
[color=blue]
> bc90021 wrote:[/color]
<<SNIP>>[color=blue][color=green]
>> 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.[/color]
>
> 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[/color]

Shawn,

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

bc90021

Closed Thread


Similar PHP bytes