str_replace and include() together

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PHPstarter
    New Member
    • Jun 2009
    • 77

    #1

    str_replace and include() together

    Hi guys.

    Here is my problem:

    I have a php file which is associated by a number/ID.

    together with BB Code my goal is to have the BB Code transfer the tags with the ID into an include of the file.

    I don't think I can use file_get_conten ts because there is PHP scripts inside the file.

    example: [file]2[/file]
    = include("file_w ith_id_2");

    But I have not found a way to do this.
    Any tips? :)
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Do you know how to operate of string?
    Code:
    <?php
    $var_id=2;
    $var_file_name='file_with_id_'.$var;
    if(file_exists($var_file_name)==false)
    {
     echo "Missing File";
     exit;
    }
    include_once($var_file_name);
    
    ?>

    Comment

    • dgreenhouse
      Recognized Expert Contributor
      • May 2008
      • 250

      #3
      Just concatenate the id onto the base file name (I assume that's what you want).

      i.e.
      Code:
      <?php // @file: variable_include.php
      for ($i=0; $i < 2; $i++)
      {
        include("variable_includes$i.php");
      }
      ?>
      
      <?php // @file: variable_include0.php
      echo 'Hello this is file: variable_includes0.php<br>';
      ?>
      
      
      <?php // @file: variable_include1.php
      echo 'Hello this is file: variable_includes0.php<br>';
      ?>
      
      Output:
      Hello this is file: variable_includes0.php
      Hello this is file: variable_includes1.php

      Comment

      • PHPstarter
        New Member
        • Jun 2009
        • 77

        #4
        Thanks for your reply. It might work, but what I was looking for is to replace a part of a larger text, into a file include.

        I have a script that handles this 'BB Code' before it's made visible to the user, so it wouldn't work to include it right there.

        example:
        Code:
        $content = str_replace("[file]2[/file]", include(file2.php), $content);

        Comment

        Working...