mrpatg
mrpatg

Reputation: 10117

using str_replace with file_get_contents

I am using file_get_contents to grab a page, but i need to replace some data in the contents of the page before echoing it.

I have this so far (this script runs on domain2.com)

<?php
$page = file_get_contents('http://domain.com/page.html');
str_replace('href="/','href="http://domain.com','$page');
echo $page;
?>

The problem is, that when the page displays, some links on the domain.com page read:

<a href=/about.html>

Which when i call in my script, are prepending it with the incorrect domain. I tried using str_replace, to look for

href="/

and replace it with

href="http://www.domain.com/

But its not working. Any clues?

Upvotes: 0

Views: 2510

Answers (2)

mrpatg
mrpatg

Reputation: 10117

fixed it

$pagefixed = str_replace("href=\"/","href=\"http://www.domain.com/","$page");

Thanks all

Upvotes: 3

jeffff
jeffff

Reputation: 1319

You'll either need to use a regular expression (preg_replace) or 2 str_replaces since quotes vary.

Upvotes: 2

Related Questions