Question

I have an application that is used to edit .txt files. It opens the selected file, writes to a file and closes the file. Each file that has to be edited has special characters e.g. [, ', =, _, ;.` that do not have to be edited so they will not be displayed on the web page when file is opened. Between those special characters text exists thats the part that will be edited.What i want to achieve is when the file is opened the special charterer will be hidden and the letters in the file only be displayed and here is the twist what i am trying to achieve is when the file is saved the special charterers will be still in the file with the edited text in there original place. Everything in the file looks like this

['text'] = ['some_,more_text']

Currently i am playing around with explode(); function and str_replace() function but so far i am only able to remove the special charterers and when the file is saved the characters do not save with the text they are removed.

Is there some function or a method that i massed that can achieve the results i am aiming for any replays are greatly appreciated

I am still going over the php manual but cant find anything close to what i need appart from explode() and str_replace().

here is my code i use this to open my file:

$fileHandle = fopen($relPath, 'r') or die("Failed to open file $relPath go and make me a sandwich! ");

echo '<form action="updatefile.php" method="post">';

while(!feof($fileHandle))
    {
    $line = fgets($fileHandle);
    $lineArr = explode('=', $line);

    if (count($lineArr) !=2){

       continue;
    }
    $part1 = trim($lineArr[0]);
    $part2 = trim($lineArr[1]);

    //$simbols = array("$", "[", "]", "'", ";");

  //echo "<pre>$part1 $part2</pre>";

           echo '<pre><input type="text" name="content_prt1[]" size="50" value="' .str_replace($simbols, "",$part1).'"> <input type="text" name="content_prt2[]" size="50" value="' .str_replace($simbols, "",$part2).'"></pre>';
    }
    echo '<input type="submit" value="Submit" name="submit1">';
    echo '<form />';

  fclose($fileHandle) or die ("Error closing file!");

this is the one that writes to file :

if(isset($_POST['submit1'])){
    ini_set('auto_detect_line_endings', TRUE);

    $file = "test_file_1.php";

    if(is_file($file))
    {
        $handle = fopen($file, "w") or die ("Error opening file! {$file}");
    }
    else
    {
        die($file . ' Dose Not Exist!');
    }

    $content_prt1 = $_POST['content_prt1'];
    $content_prt2 = $_POST['content_prt2'];

    $data = '';

    foreach ($content_prt1 as $key => $value) {
        $data .= $value . " = ". $content_prt2[$key]. PHP_EOL;
    }

    //$file_contents = $_POST["content_prt1" . "content_prt1"];

    fwrite($handle, $data) or die ("Error writing to file {$file}!");
    fclose($handle) or die("Error closing file!");

     header( 'Location:../index.php' ) ;

}
Was it helpful?

Solution

You will not find a function or Class to do this. What you are trying to archive is a lot more complicated then it looks.

Say you start with just+a=sample and display just a sample.

The user changes this to what Where should the special charactrs be placed?

+what= or +=what or what+=are all valid solutions.

You have three options:

  1. use many small text areas (ugly)
  2. createyour your own "textare" with javascript and track the users cursor position (very very bad idea)
  3. create a complicated and error prone logic to compare the string before and after edit

Usually I hate it if people publish "do not do this" answers but I see no chance to solve this problem witzh a reasinable effort.

Good luck I hope you find a sollution or different approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top