PhpStorm 2024.3 Help

Structural search and replace examples

As you know the main difference between regular search and the structural search is that in the structural search we are looking for a structural template in a programming language.

The beauty of a structural search is that you can create a pattern based on the existing template and save yourself time when searching and replacing code.

The extensive list of existing templates covers a lot of use-cases from simple patterns to more complex ones.

Each item in a pattern consists of variables that are limited by $ sign on both sides.

Method calls

The simplest template to search for method calls is $Instance$->$MethodCall$($Arguments$).

The Find tool window shows the detected method calls.

If you need to locate a method call with the specific number of arguments, place the caret at the $Arguments$ variable and configure the Count filter for it.

To navigate to the method call in the source code, double-click it in the Find tool window. PhpStorm opens the corresponding file in the editor and positions the caret at the method call.

Structural search method call.png

PHP classes

If you have a PHP class MyClass:

class MyClass {}

Then the simplest template to search for it is class $a$ {}.

Implementations of interfaces

If you have a PHP interface MyInterface and a class Implementation that implements it:

class Implementation implements MyInterface{}

Then the simplest template to search for the implementation is class $Class$ implements $SomeInterface$ {}

Descendant classes

If you have a PHP class Parent and a class Descendant that extends it:

class Descendant extends Parent{}

Then the simplest template to search for Descendant is class $Class$ extends $AnotherClass$ {}

Statements

The simplest template to search for if statements is if($var$){$code$}

As a result, the detected occurrences will be shown in the Find tool window, double-click the one you are interested in to navigate to the source code. PhpStorm opens the corresponding file in the editor and positions the caret at the statement.

Structural search if statement

Comments and string literals

The simplest template to find comments or literals containing foo is: $SomethingWeWantToFind$ or "$SomethingWeWantToFind$". To find comments/strings containing some particular words (say, foo as a word), this should be specified as a text constraint.

Examples for HTML and XML

The following examples show how you can use structural search in HTML and XML code.

Searching for XML and HTML tags, attributes, and their values

The simplest template to search for a tag is <$tag$/>.

  1. By placing constraints on the variable $tag$, you can specify tags that you want to find. For example, if you specify li, you will get all li tags.

  2. Consider the following template for searching in XML and HTML: <$tag$ $attribute$=$value$ />. For example, if you specify the text filter id for the $attribute$ variable and the \d+ regular expression as the text filter for the $value$ variable, you can find all tags that have numeric values in the id attribute.

    Searching for li tags with numeric ids

Delete all lines with the id attribute greater than 2

  1. Create an HTML file and paste the following code:

    <!doctype html> <html> <head> <title>Structural Search Example</title> <body> <ul> <li id="1">Example line 1</li> <li id="2">Example line 2</li> <li id="3">Example line 3</li> <li id="a">Example line a</li> <li id="5">Example line 5</li> </ul> </body> </html>
  2. In the main menu, go to Edit | Find | Search Structurally.

  3. From the Language list, select HTML.

  4. Paste the following string to the Search template field:

    <$tag$ $attribute$="$value$">
  5. Click the $tag$ variable.

  6. In the filter panel, click Add modifier, select Text and type li in the value field.

    If the filter panel is not visible, click Toggle Modifier Panel.

    Toggle Filter Panel
  7. Click the $attribute$ variable.

  8. In the filter panel, click Add modifier, select Text and type id in the value field.

  9. Click the $value$ variable.

  10. In the filter panel, click Add modifier, select Text and type \d+ in the value field.

    The \d+ regular expression limits search results to numeric values. So, the line with the id="a" will be filtered out.

  11. Without switching the focus from the filter panel, click the Add button, select Script and paste the following code:

    value.getText().replaceAll (/"/, '').toInteger() > 2

    The script reads the content of the $value$ variable and returns it as a string (for example, "1"). Then the script replaces all the quotes and converts the string value to integer and compares it with 2.

    Delete lines with ids greater than 2

Convert uppercase values of the class attribute in li tags to lowercase

  1. Create an HTML file and paste the following code:

    <!doctype html> <html> <head> <title class="EXAMPLE">Structural Replace Example</title> <body> <ul> <li class="EXAMPLE">Example line 1</li> <li class="example">Example line 2</li> <li class="EXAMPLE">Example line 3</li> <li class="example">Example line a</li> <li id="EXAMPLE">Example line 5</li> </ul> </body> </html>
  2. In the main menu, go to Edit | Find | Replace Structurally.

  3. From the Language list, select HTML.

  4. Paste the following string to the Search template field:

    <$tag$ $attribute$="$value$">
  5. Select the Match case checkbox.

  6. Click the $tag$ variable.

  7. In the filter panel, click Add modifier, select Text and type li in the value field.

  8. Click the $attribute$ variable.

  9. In the filter panel, click Add modifier, select Text and type class in the value field.

  10. Click the $value$ variable.

  11. In the filter panel, click Add modifier, select Text and type [A-Z].* in the value field.

    The [A-Z].* regular expression limits search results to uppercase values.

  12. From the Target list, select value. This procedure highlights all the uppercase values of the class attribute.

  13. In the Replace template field, paste the $to_lower_case$ variable.

  14. Click the $to_lower_case$ variable.

  15. In the filter panel, click Add modifier, select Script and paste the following code:

    value.getText().toLowerCase()
    Replace a target structurally
  16. Click Find.

  17. In the Find tool window, preview the found results and click Replace All.

    find.replace.all.action
Last modified: 11 February 2024