Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 21st, 2009, 12:45 PM
Coldweather Coldweather is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Location: Sweden
Posts: 48 Coldweather User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 30 m 28 sec
Reputation Power: 7
Replacing URL's in included site

Hello,

I am working on including a site with php using file_get_contents().
I have xampp set up so I run everything locally.
The page that I have included is located at sub.domain.dev while I am running the script from the main domain (domain.dev).
Of course doing this will cause all images in the included html page not to load since the img source is not a full url but "img/bg.gif".

As a regex newbie I have managed to replace the img src url of the html page with the full path so they load with this *embarrassing* code....
PHP Code:
 $site_content file_get_contents('http://sub.domain.dev/');

$match '/src="images/';
$url 'http://sub.domain.dev/';
$replace 'src="'.$url.'img';

$site_content preg_replace($match,$replace,$site_content);

echo 
$site_content

Though not fancy, it does work...

However, the css won't load due to the wrong path and even if I replaced it will the full path the image url's in the css would be pointing to the wrong source.

So I was thinking if I could get the css file content the same way as I did with the html file_get_contents(), run a preg_replace on all url's and paste it directy into the $site_content it would work.

So I see it like this...

1. Get css file contets with file_get_contents()
2. Locate the css tag in the html content variable and remove it from the string (preg_replace?)
3. Replace all url's in the content with the full http://sub.domain.dev/ with preg_replace
4. insert the css content into the head of the html content variable.

Would anyone be willing to help me with the regex patterns for this? I find them to be difficult...
Overall help and pointer also greatly appreciated, perhaps theres a better way of doing this

Cheers,
Marcus.

Reply With Quote
  #2  
Old August 23rd, 2009, 01:09 PM
Coldweather Coldweather is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Location: Sweden
Posts: 48 Coldweather User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 30 m 28 sec
Reputation Power: 7
Ok so I wrote this and got it working.
Ideas on how to optimize this is ofc welcomed, specially the regex!

Cheers

PHP Code:
 $URL 'http://sub.domain.dev/'// domain URL
$site_content file_get_contents('http://sub.domain.dev/'); // get page HTML
$css_content file_get_contents('http://sub.domain.dev/style.css'); // get CSS page

// Convert URL's in HTML to full path
$htmlImgURLPattern '/img src="/';
$htmlImgURLReplace 'img src="'.$URL.'';

$html_replaced preg_replace($htmlImgURLPattern,$htmlImgURLReplace,$site_content);

// Convert URL's in CSS to full path
$cssImgURLPattern "/url\('/";
$cssImgURLReplace "url('$URL";

$css_replaced preg_replace($cssImgURLPattern,$cssImgURLReplace,$css_content);

// Replace style link with style tag and insert converted CSS
$linkTagPattern '/<link\s(.*)rel=("stylesheet").*>/';
$linkTagReplace '<style type="text/css">'.$css_replaced.'</style>';
$resultPage preg_replace($linkTagPattern,$linkTagReplace,$html_replaced);

echo 
$resultPage

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Replacing URL's in included site

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap