Remove all single and all occurrences of <?php ***** ?>

Remove all single and all occurrences of <?php ***** ?>

12
Basic UserBasic User
12

    May 28, 2021#1

    Hi,

    Not sure how to do this with the find and replace regex and am wondering if anyone knows how to do this.

    Task 1
    I have 100+ files with php tags and code inbetween all start with <?
    then lots of php code
    and ends with ?>
    I need to replace just the first occurrence of <?php*******?> and not the rest, is this possible?

    Task 2
    I have another 100+ files with multiple php tags in the code <?php //some code ?> and would like to remove all occurrences.

    NOTE: There may or maynot be new lines /r or /n in the php code between the php tags.


    I'm not that clued up on regex and also not sure which regex can do it, ultraedit, unix or perl or any?

    Any of you guys have any ideas on how to do the above?

    Thanks

    D

      May 28, 2021#2

      Just tried a macro:

      ```
      FindInFiles "W:/page/set1/" ".html" "<?php"

      InsertMode
      ColumnModeOff
      HexOff
      UltraEditReOn

      Loop 0
      Top
      Find "<?php"
      IfFound
      StartSelect
      Find Select "?>"
      EndSelect
      Delete
      EndIf
      EndLoop
      ```

      However just results in:
      Search complete. Found '' 0 time(s). (0 file(s)).

      If I do a "find in files" via search, it finds the <?php
      Web Developer by day, DAW Tinkerer by night.

      6,633551
      Grand MasterGrand Master
      6,633551

        May 28, 2021#3

        It is absolutely no problem to use Replace in Files with the Perl regular expression search string (?s)<\?php.*?\?> to find PHP code blocks and remove them all by using an empty replace string in all files. So the second task is easy to achieve.

        But Replace in Files replaces always all strings found by the search expression and not just the first one. So it would be necessary to have something inside the first PHP code block which no other PHP code block contains ever to remove just the first code block. Or there is something before the first PHP code block which does not exist a second time in each file like <html at top of the file which can be used as an anchor to remove just the first PHP code block with the execution of an appropriate Perl regular expression Replace in Files. Or there is something after the first PHP code block which does not exist anymore a second time in each file after any other PHP code block to remove just the first PHP code block.

        Otherwise it would be necessary for the first task to code a script which first gets a list of file names, see GetListOfFiles, then runs a loop on this list to open one file after the other, replace just the first occurrence of a PHP code block, save and close the file before doing the same with next file. That would work, but is very slow in comparison to a Perl regular expression Replace in Files because of opening each file in GUI with applying all the features of UltraEdit although just a string must be removed.
        Best regards from an UC/UE/UES for Windows user from Austria

        12
        Basic UserBasic User
        12

          May 28, 2021#4

          Thanks for that Mofi :)

          Do you know the macro code to systematically go through each open file, run macro and exit on last open file?

          or if that's not possible

          Go through each open file, run macro, save and close and then when no more are open exit macro

          ?

          At least with the first one I have the option of using undo if the macro I've written has screwed up the file in someway.

          Thanks

          D
          Web Developer by day, DAW Tinkerer by night.

          6,633551
          Grand MasterGrand Master
          6,633551

            May 28, 2021#5

            I suggest reading first my reply on When to use Scripts over Macros. An UltraEdit script solution would be better for this task than an UltraEdit macro solution. However, it could be done also with a macro. See:
            Best regards from an UC/UE/UES for Windows user from Austria

            12
            Basic UserBasic User
            12

              May 28, 2021#6

              Nice 1, thanks for the info :) 
              Web Developer by day, DAW Tinkerer by night.