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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Include Specific Lines

    I have a php file that is about 20 lines (normal html and php codes lines), I want to make a new php page and in this new page I want to (using php include or other...) include the first 4 lines and then include from line number 8 to 10 and then include from line 12 to 20. And in this way lines number 5,6,7 and 11 will not be represented in this new page.
    So how can I do that?
    (any suggestions is helpfull even If using other than php include... )

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Well, you could use eval(), but god kills a kitten every time you use it.

    PHP Code:
    $file file('filename.inc');
    // -1 for clarity. Array is 0 based, so line 5 is array entry 4
    unset($file[5-1], $file[6-1], $file[7-1], $file[11-1]);
    file_put_contents('newfilename.inc'implode(''$file));
    include(
    'newfilename.inc'); 
    Untested.

    Might I also add: What an odd request. Isn't there a better way to do it?
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Lamped View Post
    Well, you could use eval(), but god kills a kitten every time you use it.

    PHP Code:
    $file file('filename.inc');
    // -1 for clarity. Array is 0 based, so line 5 is array entry 4
    unset($file[5-1], $file[6-1], $file[7-1], $file[11-1]);
    file_put_contents('newfilename.inc'implode(''$file));
    include(
    'newfilename.inc'); 
    Untested.

    Might I also add: What an odd request. Isn't there a better way to do it?
    Ok, it seems that it is working but now I want how to remove a word from a line?

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    $file[1] = str_replace('word'''$file[1]); 
    Removes all occurrences of "word" in the second line of the file.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Either create a specific include file for that page, or set a var and use code blocks to determine which parts get used in the include file, i.e:

    page.php, (and any other relevant files):

    Code:
    if (!defined('mainpage'))
    {
        define('mainpage', true);
    }
    otherpage.php:

    Code:
    if (!defined('otherpage'))
    {
        define('otherpage', true);
    }
    then in the included file:

    Code:
    if (defined(mainpage))
    {
        [use this code]
    }
    
    if (defined('otherpage'))
    {
        [use this code]
    }
    If you need different words appearing in the include output depending on where it's called from, set and use vars for output.
    Last edited by MattF; 12-01-2010 at 06:21 PM.

  • #6
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I have a php file that is about 20 lines (normal html and php codes lines), I want to make a new php page and in this new page I want to (using php include or other...) include the first 4 lines and then include from line number 8 to 10 and then include from line 12 to 20. And in this way lines number 5,6,7 and 11 will not be represented in this new page.

    Quote Originally Posted by Lamped View Post
    Well, you could use eval(), but god kills a kitten every time you use it.

    PHP Code:
    $file file('filename.inc');
    // -1 for clarity. Array is 0 based, so line 5 is array entry 4
    unset($file[5-1], $file[6-1], $file[7-1], $file[11-1]);
    file_put_contents('newfilename.inc'implode(''$file));
    include(
    'newfilename.inc'); 
    Untested.

    Might I also add: What an odd request. Isn't there a better way to do it?
    Ok, I re-tested this code and is isn't working for Removing specific lines, so can Anyone correct this code or have a new one?

    Any Suggestion Will be helpful
    Last edited by Tony M; 12-20-2010 at 05:50 PM.


  •  

    Posting Permissions

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