我有這個字符串:檢查是否字符串包含任何文本
$mystring = "SIZE,DETAIL";
而且I'm使用:
@if (strpos($mystring, 'SIZE'))
{{ $item->size }}
@endif
@if (strpos($mystring, 'DETAIL'))
{{ $item->detail }}
@endif
但這個工作正常大小,但不與細節。
這裏有什麼問題?
我有這個字符串:檢查是否字符串包含任何文本
$mystring = "SIZE,DETAIL";
而且I'm使用:
@if (strpos($mystring, 'SIZE'))
{{ $item->size }}
@endif
@if (strpos($mystring, 'DETAIL'))
{{ $item->detail }}
@endif
但這個工作正常大小,但不與細節。
這裏有什麼問題?
這個函數可以返回布爾FALSE ,但也可能返回一個非布爾值,其值爲FALSE。
試試這個:
@if (strpos($mystring, 'SIZE') !== false)
{{ $item->size }}
@endif
@if (strpos($mystring, 'DETAIL') !== false)
{{ $item->detail }}
@endif
使用strpos
時,您需要將其與FALSE
進行比較。的刀片代碼的一個例子是:
@if (strpos($mystring, 'SIZE') !== FALSE)
{{ $item->size }}
@endif
@if (strpos($mystring, 'DETAIL') !== FALSE)
{{ $item->detail }}
@endif
然而,在使用Laravel的時候,你可以用str_contains($haystack, $needles)
而不是strpos
。
由於您使用Laravel,您可以使用str_contains()
幫手:
@if (str_contains($mystring, 'SIZE'))
的
str_contains
函數確定給定的字符串包含給定的值
也許嘗試http://php.net/manual/en/function.preg-match.php - 和至極Laravel版本你真的使用? –