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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2004
    Location
    Sydney, Australia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Fastest way for a string replace in document

    Lets say that in your document you typed $theme and you wished that text to be replaced with a word of your choice via PHP. What would be the easiest way without having to put your HTML in the <?php ?> tags?

    Example:
    Code:
    <?php 
    
    script (or link to external script)
    
    ?>
    
    <html>
    <body>
    My new theme today is called $theme. I hope you like $theme!
    </body>
    </html>
    Thanks

  • #2
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Load the file and echo it out. That's the way I do my website ...

    Pretty simple really ...
    PHP Code:
    function LoadTemplate($strFile) {
     if( !
    file_exists $strFile ) )
        die(
    "File '$strFile' does not exist or access permissions disallow access!");
     
    $handle fopen $strFile,  "r" );
     return 
    fread$handlefilesize$strFile ) );

    That function returns the content of the file and checks to make sure it's there first ....

    Then just do something like --
    PHP Code:
    $page LoadTemplate("your_file.htm");
    echo 
    str_replace("\$theme",$theme,$page); 
    [edit:] A few syntax modifications
    Last edited by Mhtml; 01-17-2004 at 03:26 AM.
    Omnis mico antequam dominus Spookster!

  • #3
    New to the CF scene
    Join Date
    Jan 2004
    Location
    Sydney, Australia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, lets say that you wanted to expand and put several strings to be replaced over many documents. is there a way without putting the filename in the script?

  • #4
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hrmm .. well that makes it more complex, especially if you want to do it over many pages without putting the filename in the file ...The best way would be to use basically the same method ...

    If you were to use regexp you could just search for anything not in <?php ?> delimeters that matches the variable you want to replace and replace it ... would this be acceptable?
    Omnis mico antequam dominus Spookster!


  •  

    Posting Permissions

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