09-09-2003, 03:05 PM
is it possible to use preg_replace to change the text in an included file?
file to be included has this text:
I want to replace all instances of 'a href="/' with 'a href="http://myserver.com/' in the included file so when the file is included, it will have the following
Is that possible?
09-09-2003, 06:09 PM
You could set up a variable just before the include to specify the path or to pull it from a url or form.
$location = "http://www.myserver.com/"
then in the include file set it to
09-09-2003, 06:41 PM
or if you needed to use preg_replace, you could do so like this...
$domain = "http://www.mysite.com";
preg_replace("/\\<a\\s(.*)href=\\"(.*)\\"/is", "<a $1href=\\"".$domain."$2\\"", $contents);
where $contents is the code to apply the relplace... I think that should work anyway... If its anythin like usual, mordred will come along with some amazing regex :D
09-09-2003, 09:00 PM
There's probably a simple solution to this problem, provided you actually display the file contents in a browser. So no amazing regex from me right now, must save energy. :D
$domain = 'http://www.mysite.com';
$contents = str_replace('</head>', "<base href=\"$domain\"></head>", $contents);
Just utilizing the power of HTML 3.2 and putting a <base> address into the header part should force all browsers to resolve all links from the $domain address. If that does work for you, it would also solve the problem of distinguishing between absolute/relative links in various attributes (href, src,). Give it a try.
09-09-2003, 09:45 PM
There is something about me... I can never give the simplest solution... ah well.. nice 1 mordred.
09-10-2003, 12:33 PM
Actually, I think you're assuming I have access to modify the included file, which I do not.
The included file is actually a .cgi file for a search script on another server in our intranet. The problem is that when the search results are displayed, all the links are relative.
To better explain the problem, I've got a file on my server named search.php
The contents are basically this...
$term = urlencode($_POST['keyword']);
$file = "http://otherserver.com/cgi-bin/search.cgi?keyword=".$term;
The search.cgi file actually returns a complete html file, so I also need to dump the <html> -> <head> junk as well. I was trying to get something like this to go, but it doesn't seem to work.
$content = include($file);
preg_replace("/\<a\s(.*)href=\"(.*)\"/is", "<a $1href=\"".$domain."$2\"", $content);
09-10-2003, 05:02 PM
im assuming that you mean that the cgi file returns a page with <html></html>...
what you have done there will not work (as far as i know), because include puts the entire file and adds it to the page...
you could try:
$cont = ob_get_contents();
but i dont know if that would work...
09-10-2003, 05:08 PM
That would work, missing-score. It's also a common idiom to capture the output of an included file via ob_get_contents(), and it allows bcarl to execute the regex solutions we posted on the captured output.
09-10-2003, 05:30 PM
cool. ive used output buffers before, just didnt know how included files worked within that. thanks.