Search code examples
phpphpstorm

PhpStorm 2016.2 find and replace multiline text


In PhpStorm 2016.2 I have a new project that has been inherited and [badly] needs updating.

There are many pages each with opening line like so (example):

<?
include ("/inc/db.php");

I need to replace this line with several lines such as:

<?php
include "siteheader.php";
require "class.myclass.inc.php";
$dataBase = new DbObj();

I have previously simply copy and pasted multiline code into the PhpStorm search/replace function and that's (usually but not always) returned the correct changes, although they're all squished into single lines, making them harder to read (EOL characters are removed).

  • In this instance am looking specifically at the "replace in path" function as I need to apply this change to many pages.

I have Read the manual but can see no option for this. I think I could possibly use a Regular Expression but this would not be ideal (escapings etc.).

I have also looked but not found a suitable plugin from the PhpStorm Plugin Repository.

Is there a way of searching and/or replacing multiline text in path in PhpStorm 2016.2?

Cheers


Solution

  • There is no easy to use multi-line search or replace across multiple files (Find/Replace in Path functionality) unfortunately.

    Right now you have to use Regex option for that -- that's the only option that works.

    Watch these tickets (star/vote/comment) to get notified on any progress in this regard.


    Manually making regex-compatible text can be quite problematic .. therefore you might use this few-steps trick:

    1. Type your new text in one file to start with
    2. Select such text and invoke Replace in Path... dialog -- with Regex option pre-selected it should automatically escape your selection to be regex-compatible
    3. Copy that already-escaped text somewhere (just Clipboard should be enough)
    4. Close dialog and go back to original file
    5. Select text you want to replace and invoke Replace in Path... dialog -- it will have your initial text already filled in and regex compatible
    6. Paste previously copied escaped text into Replace field
    7. Execute find/replacement

    On related note: https://stackoverflow.com/a/38672886/783119