test1321
test1321

Reputation: 331

how to find string in laravel blade template

How can I check data using something like strpos

data->url = www.youtube.come/12345

@if($data->url != 'youtube' )

@endif

updated  how can i put this in my balde temp

preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+|(?<=/videos/)[^&^/\n]+#", $line['YOUTUBE'], $matches);

        $videoId = $matches[0];

        if(substr_count($line['YOUTUBE'], "facebook.com") > 0){
          print "<div class=\"fb-video\" data-href=\"{$line['YOUTUBE']}\" data-allowfullscreen=\"true\" data-width=\"600\"></div>";
        }else{
          print "<iframe width=\"600\" height=\"350\" frameborder=\"0\" src=\"https://www.youtube.com/embed/{$videoId}?autoplay=1\"></iframe>";
        }

Upvotes: 3

Views: 8127

Answers (2)

Tpojka
Tpojka

Reputation: 7111

You can use strpos(), strstr() (or their case insensitive variants when appropriate) but also you can use Laravel's str_contains() function which is basically using strpos() PHP inbuilt method:

$contains = str_contains($line['YOUTUBE'], "facebook.com");

Upvotes: 4

Junaid Ahmad
Junaid Ahmad

Reputation: 599

You can use any php function within blade too... see example below:

 @if (strpos($data->url, 'youtube') !== false) {
   echo 'true';
 @endif

Update: In some situations, it's useful to embed PHP code into your views. You can use the Blade @php directive to execute a block of plain PHP within your template:

@php
//
@endphp

While Blade provides this feature, using it frequently may be a signal that you have too much logic embedded within your template. Reference: https://laravel.com/docs/5.4/blade#php Hope it will help you. :)

Upvotes: 3

Related Questions