Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string contain any text

I have this string:

$mystring = "SIZE,DETAIL";

And I´m using:

@if (strpos($mystring, 'SIZE'))
        {{ $item->size }}
@endif
@if (strpos($mystring, 'DETAIL'))
        {{ $item->detail }}
@endif

But this works fine with SIZE, but not with DETAIL.

What is the problem here?

like image 817
RichardMiracles Avatar asked Jan 17 '17 10:01

RichardMiracles


Video Answer


2 Answers

Since you're using Laravel, you can use str_contains() helper:

@if (str_contains($mystring, 'SIZE'))

The str_contains function determines if the given string contains the given value

like image 128
Alexey Mezenin Avatar answered Oct 10 '22 18:10

Alexey Mezenin


This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.

Try this:

@if (strpos($mystring, 'SIZE') !== false)
    {{ $item->size }}
@endif
@if (strpos($mystring, 'DETAIL') !== false)
    {{ $item->detail }}
@endif

refer: http://php.net/manual/en/function.strpos.php

like image 41
mith Avatar answered Oct 10 '22 20:10

mith