2

I have the following 2 lines of code in my blade template:

<p>
    <?php preg_match('/<p>(.*?)<\/p>/i' , $recent->blog_content , $matches) ?>
    {!! str_limit($matches[0] , 50 , '...') !!}
</p>

Now how do i write the first like of code I.E. the below line of code:

<?php preg_match('/<p>(.*?)<\/p>/i' , $recent->blog_content , $matches) ?>

In blade as both the below two syntaxs , outputs the result to the frontEnd instead of execute my code:

{{ preg_match('/<p>(.*?)<\/p>/i' , $recent->blog_content , $matches) }}


{!! preg_match('/<p>(.*?)<\/p>/i' , $recent->blog_content , $matches) !!}

Basically how do i execute multiple lines of PHP code in blade ?

7
  • i havent used this "blade" thing, but a ";" between instructions is not enought? try it. Commented Jan 17, 2017 at 7:41
  • You mean this? {{$recent->blog_content}}, {{$matches}} Commented Jan 17, 2017 at 8:40
  • 1
    It is bad idea to use php code inside blade template.. Instead you can process data into controller and then pass it to blade. Blade is not meant to process data. Commented Jan 17, 2017 at 9:08
  • are you wanted to print both two lines or there is any condition. Commented Jan 17, 2017 at 9:29
  • @jyotimishra i would like to change this line of code <?php preg_match('/<p>(.*?)<\/p>/i' , $recent->blog_content , $matches) ?> to blade Commented Jan 17, 2017 at 10:02

1 Answer 1

2

You can use the Blade @php directive to execute a block of plain PHP within your template:

@php
    $counter = 1;
@endphp
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.