...

View Full Version : Merge text in a textfile



aceooo2
03-28-2007, 01:14 AM
Hey all, I have created code but it is incomplete.
I need to know how I could select two sections of text and merge them into one. for example;

if i have a textfile with

banana
Pear
TV
Orange
Window


and i select lines 1 and 2 to merge with 5 I would get

TV
Orange
Window banana Pear,,


I have created 4 textboxes and it also reads the file with line numbers so its easier for the user to choose which lines they need.Its a bit hairy at the moment because im trying different things and come a ground.
Heres the code:-


<form method="post">
On line
<input type="text" name="line1">
<br>Before line
<input type="text" name="line2">
<br> Merge with
On line
<input type="text" name="line3">
<br>Before line
<input type="text" name="line4">

<br><input type="submit">
</form>

<?php
$l1 = $_POST["line1"];
$l2 = $_POST["line2"];
if (isset($l1) && isset($l2)) {
$m = file_get_contents("testfile.txt");
$lines = explode("\n",$m);
if ($l2 < count($lines)) {
for ($i = $l1;$i < $l2;$i++) {
echo $lines[$i]."\n<br>";
}
}
else {
echo "Query must be less then: ".count($lines);
}
}

?>
<!--Second entry -->
<?php
$l3 = $_POST["line3"];
$l4 = $_POST["line4"];
if (isset($l3) && isset($l4)) {
$m = file_get_contents("testfile.txt");
$lines = explode("\n",$m);
if ($l4 < count($lines)) {
for ($i = $l3;$i < $l4;$i++) {
echo $lines[$i]."\n<br>";
}
}
else {
echo "Query must be less then: ".count($lines);
}
}

?>

<P>Read below to gather the correct line numbers to delete (soon text search)<br><br>
<?php
$strings = file('testfile.txt'); //Getting the file into an array by using file ()
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars(trim($line,"\r\n")) . "<br />\n";
}
?>
can anyone help me as I am stuck. . : s



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum