← Blog overview

Converting PHP Short Echo Tags to Blade Templates in Laravel

In the world of web development, working on various frameworks and technologies is an adventure in itself. In one of my work escapades, I found myself tasked with converting plain old PHP+HTML templates to Laravel's blade templates. This transition required me to replace PHP's short echo tags with Laravel's Blade syntax. In this guide, I'll share my experience and the regex solution I used for this conversion.

Understanding PHP Short Echo Tags

Before diving into the conversion process, let's clarify what PHP short echo tags are. PHP short echo tags allow developers to quickly echo a variable's value within HTML templates. They use the <?= expression ?> syntax to achieve this.

The Conversion Challenge

Converting PHP short echo tags to Blade templates may seem daunting, especially if you have numerous templates to update. However, it's a necessary step to ensure your project aligns with Laravel's conventions.

The Regex Solution

To automate this process, I used a regular expression (regex) pattern to locate and replace PHP short echo tags. The regex pattern I employed is as follows:

<\?=\s*([^)]*\)).*?;?\s*\?> Here's a breakdown of what this regex pattern does:

  • <\?=: This part of the pattern matches the opening <?= tag.
  • \s*: This part matches any amount of whitespace characters (spaces, tabs, etc.) after the opening tag.
  • ([^)]*\)): This is the capturing group. It captures everything inside the opening <?= ... ?> tags. Here's how it works:
    • (: This is the beginning of the capturing group.
    • [^)]*: This part matches any characters that are not a closing ). [^)] is a character class that matches any character except ), and * allows it to match zero or more such characters.
    • \): This part matches the last ) character, so it is included in the capturing group. The ) outside of the character class is used to specifically match a closing parenthesis.
  • .*?: This part matches any characters (except newline) reluctantly. The *? is a non-greedy quantifier, which means it will try to match as few characters as possible.
  • ;?: This part matches an optional semicolon (;) if it is present in the input text.
  • \s*: This part matches any amount of whitespace characters (spaces, tabs, etc.) after the optional semicolon.
  • \?>: This part matches the closing ?> tag.

In summary, the capturing group ([^)]*\)) captures everything inside the <?= ... ?> tags up to and including the last ) before an optional semicolon and the closing ?> tag. This allows you to extract the content inside the tags while ensuring that the last ) is included in the capture group.

Replacing with Blade Syntax

Once you've identified the PHP short echo tags using the regex pattern, the next step is to replace them with Blade syntax. In PHPStorm or any other code editor, you can use the following replacement syntax: {{ $1 }} Here's what this Blade syntax does:

  • {{: This indicates the start of a Blade expression.
  • $1: This references the content captured within the parentheses in the regex pattern. It corresponds to the expression within the PHP short echo tag.
  • }}: This marks the end of the Blade expression.


Converting PHP short echo tags to Laravel's Blade templates may seem like a challenging task, but with the right regex pattern and a code editor that supports regex find and replace, it can be accomplished efficiently. This conversion ensures that your templates align with Laravel's syntax, making your project more maintainable and in line with best practices.