I have following $content
string. I has title
and optionally the icon
values also. I want to get all the values of titles, but I want to check there is an icon for that title, then I want to get that also.
I can get all titles using the preg_match_all
. However I am unable to find out how can I check if there's icon for that, and display it.
I'm trying following code:
$content = '[sc-item title="Item1 Title" icon="icon-bullet"] Lorem ipsum dolor sit amet[/sc-item][sc-item title="Item2 Title"] Lorem [/sc-item]';
$titles = array();
preg_match_all( '/sc-item title="(.*?)"/i', $content, $matches, PREG_OFFSET_CAPTURE );
preg_match_all( '/icon="(.*?)"/i', $content, $icon_matches, PREG_OFFSET_CAPTURE );
if( isset($matches[1]) ){
$titles = $matches[1];
}
if( isset($icon_matches[1]) ){
$icons = $icon_matches[1];
}
foreach( $titles as $title ){
echo $title[0];
//echo $icon[0];
}
In the above example, the first title has and icon next to it, while the second title does not. So I will like to get the first title+icon and second title only.