If you don't already have a getBlockContent() helper, you can create one.
Add this to app/Helpers/helpers.php (or wherever your global helper functions are):
if (!function_exists('getBlockContent')) {
function getBlockContent($content, $label)
{
preg_match_all('/<!--label:(.*?)-->(.*?)(?=(<!--block-->|$))/s', $content, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
if (trim($match[1]) == $label) {
return trim($match[2]);
}
}
return '';
}
}
{{ getBlockContent($post->content, 'Label') }}
{!! getBlockContent($post->content, 'Short Description') !!}
{{ getBlockContent($post->content, 'Tags') }}
Since you're calling it multiple times for the same post, it's more efficient to parse the content once.
if (!function_exists('getContentBlocks')) {
function getContentBlocks($content)
{
preg_match_all('/<!--label:(.*?)-->(.*?)(?=(<!--block-->|$))/s', $content, $matches, PREG_SET_ORDER);
$blocks = [];
foreach ($matches as $match) {
$blocks[trim($match[1])] = trim($match[2]);
}
return $blocks;
}
}
@php
$blocks = getContentBlocks($post->content);
@endphp
{{ $blocks['Label'] ?? '' }}
{!! $blocks['Short Description'] ?? '' !!}
@foreach(explode(',', $blocks['Tags'] ?? '') as $tag)
<span class="tag">{{ trim($tag) }}</span>
@endforeach
This second approach is recommended because it parses the content only once per post instead of running the regular expression three separate times. It will be noticeably more efficient if you have many posts.