...

View Full Version : Include Specific Lines



Tony M
12-01-2010, 03:23 PM
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... :thumbsup:)

Lamped
12-01-2010, 03:36 PM
Well, you could use eval(), but god kills a kitten every time you use it.



$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?

Tony M
12-01-2010, 04:40 PM
Well, you could use eval(), but god kills a kitten every time you use it.



$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?

Lamped
12-01-2010, 04:58 PM
$file[1] = str_replace('word', '', $file[1]);

Removes all occurrences of "word" in the second line of the file.

MattF
12-01-2010, 05:17 PM
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):



if (!defined('mainpage'))
{
define('mainpage', true);
}


otherpage.php:



if (!defined('otherpage'))
{
define('otherpage', true);
}


then in the included file:



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.

Tony M
12-20-2010, 04:30 PM
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.


Well, you could use eval(), but god kills a kitten every time you use it.



$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 :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum