Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of $1 and $2 in the Blade::extend function

I saw this example in the Laravel Docs:

Blade::extend(function($view, $compiler)
{
    $pattern = $compiler->createMatcher('datetime');
    return preg_replace($pattern, '$1<?php echo $2->format(\'m/d/Y H:i\'); ?>', $view);
});

but I don't seem to understand it, and some time the examples in the web include $3.

I didn't find a proper answer to this through a Google search, I appreciate any help.

like image 203
Mo Kawsara Avatar asked Jan 20 '15 14:01

Mo Kawsara


1 Answers

It's a string replacement! It replaces the variable (e.g. $1) with the matching group of the pattern!

As example (Pseudo Code):

$pattern = "/(.*?)([a-z])/";
//Here is $1 ^ This group and $2 would be the second group

Also as additional help:

PHP regex Cheat Sheet

Online Regex tester (<- It visualizes the match of your regex very nice and explains the different parts of your regex)

like image 153
Rizier123 Avatar answered Nov 10 '22 00:11

Rizier123