file_get_contents
returns a string containing the file's content.
So, you can work in this string using whichever string manipulation function you'd want, like the ones you talked about.
Something like this, using str_replace, would probably do :
$content = file_get_contents('http://www.google.com');
$new_content = str_replace('<a href="', '<a href="site.php?url=', $content);
echo $new_content;
But note it will only replace the URL in the href
attribute when that attribute is the first one of the <a
tag...
Using a regex might help you a bit more ; but it probably won't be perfect either, I'm afraid...
If you are working with an HTML document and want a "full" solution, using DOMDocument::loadHTML
and working with DOM manipulation methods might be another (a bit more complex, but probably more powerful) solution.
The answers given to those two questions might also be able to help you, depending on what you are willing to do :
EDIT after seeing the comment :
If you want to replace two strings, you can pass arrays to the two first parameters of str_replace
. For instance :
$new_content = str_replace(
array('<a href="', 'Pages'),
array('<a href="site.php?url=', 'TEST'),
$content);
With that :
- '
<a href="
' will be replaced by '<a href="site.php?url=
'
- and '
Pages
' will get replaced by 'TEST
'
And, quoting the manual :
If search and replace are arrays,
then str_replace() takes a value from
each array and uses them to do search
and replace on subject . If replace
has fewer values than search , then an
empty string is used for the rest of
replacement values. If search is an
array and replace is a string, then
this replacement string is used for
every value of search .
If you want to replace all instances of '<a href="
', well, it's what str_replace
does by default :-)