Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts

    preg_replace on an included file.

    is it possible to use preg_replace to change the text in an included file?

    Example:

    file to be included has this text:

    Code:
    <a href="/my/path/to/file.php">link</a>
    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

    Code:
    <a href="http://myserver.com/my/path/to/file.php">link</a>
    Is that possible?

  • #2
    New Coder
    Join Date
    Sep 2003
    Location
    Cardboard Box
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    <a href=\"".$location."my/path/to/file.php\">link</a>

    hth

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    or if you needed to use preg_replace, you could do so like this...

    PHP Code:

    $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

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    $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.
    De gustibus non est disputandum.

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    heh..

    There is something about me... I can never give the simplest solution... ah well.. nice 1 mordred.

  • #6
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    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...

    Code:
    <html>
    ...
    <body>
    <?php
       $term = urlencode($_POST['keyword']);
       $file = "http://otherserver.com/cgi-bin/search.cgi?keyword=".$term;
       inculude($file);
    ?>
    ...
    </body>
    </html>
    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.

    Code:
    $content = include($file);
    preg_replace("/\<a\s(.*)href=\"(.*)\"/is", "<a $1href=\"".$domain."$2\"", $content);
    echo $content;

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:

    ob_start
    ();

    include(
    $file);

    $cont ob_get_contents();

    ob_end_clean();

    echo 
    $cont... 
    but i dont know if that would work...

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    De gustibus non est disputandum.

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cool. ive used output buffers before, just didnt know how included files worked within that. thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •