I've been searching google for about the last hour now and have yet to find an answer to my problem.

Basically, using PHP, I need to be able to open a specific local file, search that file for a specific string/function, input/write dynamically generated code from an input to a specific line based on the location of the found string/function.

I know this might seem confusing, but the end result is a controlled user being able to add an additional case/if type function to existing ones in a file. It will have to happen in multiple files, but the base idea remains the same.

I've looked at using fopen/fwrite but i haven't found a solid answer that would suit my needs.

Thanks in advance for your help.

    you'll need to use a combination of filesystem functions to get your results..

    first

    fopen - to open the file
    fgets - to read line by line

    and a plethora of other functions to get your desired result..

    could you explain alil more or possibly show some code your using now

      [man]file_get_contents/man can load the file into a string variable via one command. From that point, I have no specific recommendation on how to manipulate the contents without a better understanding of the requirements, though it might be a case for something like [man]str_replace/man or [man]preg_replace/man. Then just write the contents back via [man]file_put_contents/man.

        efficacious;10882794 wrote:

        Nog why does th dog on your sig look sick?

        Just looks sleepy to me -- which is what he does most of the time, so I figured it was a representative picture. :p

          For instance,

          I need to be able to search "file.php" for something like:

          case 'casename' : Name::function(); break;
          

          and add an additional case below, or just delete that case.

          Ive tried this code:

          $search = "case 'casename' : Name::function(); break;";
          
          $toBeAdded = "case 'newcasename' : Name::function(); break;";
          
          $input = $search . $toBeAdded;
          $file = file_get_contents(file.php);
          $newfile = preg_replace($search,$input,$newfile);
          file_put_contents(file.php,$newfile);
          

          and a few variations using preg_match and such

          Now the main road block i keep hitting is it doesn't error out, but it wont output anything to file.php. However, i know that the concept is working cause if i just use a search string with out ":" or ";" it works fine, even if $toBeAdded contains those characters. I've also tried using : and \; to treat them as literal characters... but same result.

          With incorporating preg_match it will error and output that it was because of a : or ;

          What i'm wondering is,
          1. Am i totally backwards on how im attempting this?
          2. is there a better (working) way to achieve what im attempting to do?
          3. Is this even going to be possible?

          Im open to any suggestions.

          Thanks again for the help!

            If you have your PHP running on a Linux system (who in their right mind would choose Windows?!) then you can use the exec() function to call the Grep command,

            grep string_to_find filepath

            and the filepath can contain wildcards, like "grep html ."

              Most of you errors using preg_match and their ilk would be as a result of not having pattern delimiters around the pattern you're using (in fact, since you're searching for a single specific string, preg_match would be overkill, and strpos would find it quite adequately).

              Assuming the string you're looking for is on a single line:

              $input = fopen('file.php, 'r');
              $output = fopen('file.php.out, 'w');
              while(!feof($input))
              {
                  $line = fgets($input);
                  fwrite($output, $line);
                  if(strpos($line, 'what you are looking for')!==false)
                      fwrite($output, "the new line\n";
              }
              fclose($input); fclose($output);
              

              Then you have the option of deleting the input file and renaming the output file.

                If you use strpos to find it, then wouldn't it be easier to use the PHP file() function to read the entire file into an array, which is less code than what you suggest. Obviously, it wouldn't work if the file in question was too large, but I think it might work in this case.

                  Probably; I was reading the file line by line precisely because it might be large. If I was going to use file() to read the whole thing at once I'd use preg_grep() to search for the representative line. Take the key from the resulting array (assuming it's there), write the slice of the array up to and including that key, write the new line, and write the rest of the array.

                    Write a Reply...