PHPStorm – how to replace HTML tags using regex? - createIT
Get a free advice now!

    Pick the topic
    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.
    Tags

    PHPStorm – how to replace HTML tags using regex?

    PHPStorm – how to replace HTML tags using regex?

    Challenge: search for the last table column and replace it with a new value

    Solution: use proper regex and click ‘Replace All’

    We have an HTML document with table data. We need to remove the last column. We could do it manually, but our table has over 200 rows. How to automate the “search and replace” job?

    PHPStorm includes an option to find a particular string using a regex formula. Let’s formulate a proper one. The column for removal is placed as the last element in TR tags. It always contains a number value. We should also remember that TD elements are preceded by empty spaces.

    Regex for the last column

    We’re going to use regex that will find table cells with numbers, we’re also looking for the closing TR tag. As a new replacement, we are going to use the closing TR tag.

    Here is the final solution:

    Search for:

            <td>[+-]?([0-9]*[.])?[0-9]+</td>
        </tr>
    

    Replace with:

        </tr>

    Enable the regex replacement

    Make sure to enable Regex mode icon (it’s next to the search field. Look for a dot with the asterisk icon).

    Dark window with code and an arrow pointing to one of the options

    Example table with data

    Lastly, here is the HTML source from our example.

    <table width="900px">
        <tbody>
        <tr>
            <th width="150px">First Name</th>
            <th width="150px">Last Name</th>
            <th width="150px">Age</th>
            <th width="150px">Education</th>
            <th width="150px">Occupation</th>
            <th width="150px">last_col</th>
        </tr>
        <tr>
            <td>Max</td>
            <td>Thomas</td>
            <td>26</td>
            <td>Upper secondary</td>
            <td>Composer</td>
            <td>967</td>
        </tr>
        <tr>
            <td>Adison</td>
            <td>Cameron</td>
            <td>24</td>
            <td>Upper secondary</td>
            <td>Producer</td>
            <td>910</td>
        </tr>
        </tbody>
    </table>
    
    Dark window with colorful code

    That’s it for today’s tutorial. Be sure to follow us for other useful tips and guidelines.

    Comments
    0 response

    Add comment

    Your email address will not be published. Required fields are marked *

    Popular news

    Automating stock and price updates in WooCommerce
    • Dev Tips and Tricks

    Automating stock and price updates in WooCommerce

    September 23, 2024 by createIT
    Integrating advanced subscription features in WooCommerce
    • Dev Tips and Tricks

    Integrating advanced subscription features in WooCommerce

    September 16, 2024 by createIT
    Fetching Time records from ActiveCollab API
    • Dev Tips and Tricks

    Fetching Time records from ActiveCollab API

    September 9, 2024 by createIT
    Docker Compose for PrestaShop
    • Dev Tips and Tricks

    Docker Compose for PrestaShop

    September 2, 2024 by createIT
    WordPress wizard in admin – step by step
    • Dev Tips and Tricks

    WordPress wizard in admin – step by step

    August 29, 2024 by createIT
    Order Status Sync between PrestaShop and External APIs
    • Dev Tips and Tricks

    Order Status Sync between PrestaShop and External APIs

    August 26, 2024 by createIT
    What is PHP used for in web development 
    • Dev Tips and Tricks

    What is PHP used for in web development 

    August 22, 2024 by createIT
    Automating WooCommerce product availability date
    • Dev Tips and Tricks

    Automating WooCommerce product availability date

    August 15, 2024 by createIT
    WP Quiz Adventure – FAQ
    • Dev Tips and Tricks

    WP Quiz Adventure – FAQ

    August 12, 2024 by createIT

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us