Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm 2016.2 find and replace multiline text

Tags:

php

phpstorm

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

like image 982
Martin Avatar asked Aug 30 '16 11:08

Martin


2 Answers

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.

  • https://youtrack.jetbrains.com/issue/IDEA-69435
  • https://youtrack.jetbrains.com/issue/IDEA-61925
  • https://youtrack.jetbrains.com/issue/IDEA-145720

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

like image 193
LazyOne Avatar answered Sep 27 '22 20:09

LazyOne


You can do multiline Find&Replace with Regex option turned on

Find:

<\?\ninclude \("/inc/db\.php"\);

Replace:

<?php\ninclude "siteheader.php"; \nrequire "class.myclass.inc.php"; \n\$dataBase = new DbObj();

As you can see you need to do some additional work to escape some special characters and put \n instead of new lines, but it works. I've just checked.

P.S. Indeed, it was possible to simply paste multiline text in previous versions, but it's not possible anymore. ;-(

like image 24
Jakub Matczak Avatar answered Sep 27 '22 20:09

Jakub Matczak