I've created a set of custom tags that go inside their own wrapper (see example below & there can be many different tags on a page at once) that I'm trying to find the best method for getting the tags and their info, and easiest for the server to process.
All the content I've been able to find on this is dated a few years at least and I figured maybe someone in the community would have ideas to help point me in a better direction.
[sl_heading tag='h3' padding='10' heading='Our Most Recent Posts' color='' style='' size='' subheading_active='' subheading_size='15' custom_class=''][/sl_heading ]
[sl_posts posts_type='general' categories='24' link='category' posts_style='posts-grid' columns='4' contents='title_read_more' content_length='excerpt_read_more' preview_mode='auto' image_size='thumbnail' items='-1' offset='0' paginate='no']
So basically what I'm trying to do is the editor saves this to the database, and I'm trying to efficiently and quickly parse this out to the proper code on the website side. So my method is detecting the tag, determine what it is and what the tag values are, and show the proper HTML code in return.
I've already got everything but the parsing out the code part done, and I'm stuck on how to properly search and find the tags and their values effectively.
PS. Any help is appreciated, and I've searched through a lot of SO posts so what I'm trying to do hasn't been asked about in years (outdated posts) for what I can only understandably assume were much earlier versions of PHP.
So I took a lot of time out and studied a few different systems that are open source, and figured out an effective, and fast working solution. I took a simple function I seen and reworked it to work for me. This finds all instances of substring text I look for (something like [start] and [end] ) and gets all the content between it for me. It then replaces that entire string and replaces it with the appropriate content.
See code below, copy paste into PHPfiddle.org for quick run.
<?php
function getContents($str, $startDelimiter, $endDelimiter) {
$contents = array();
$startDelimiterLength = strlen($startDelimiter);
$endDelimiterLength = strlen($endDelimiter);
$startFrom = 0;
$contentStart = 0;
$contentEnd = 0;
while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
$contentStart += $startDelimiterLength;
$contentEnd = strpos($str, $endDelimiter, $contentStart);
if (false === $contentEnd) { break; }
$content_length = $contentEnd - $contentStart;
$the_content = substr($str, $contentStart, $content_length);
$replace_this = "$startDelimiter$the_content$endDelimiter";
$contents[] = array($the_content, $replace_this);
$startFrom = $contentEnd + $endDelimiterLength;
}
return $contents;
}
$thiss = "This is a test text area, filled with dummy text such as: Lorem ipsum dolor sit amet. [advertisers-display]123[/advertisers-display] This is a test text area, filled with dummy text such as: Lorem ipsum dolor sit amet. [advertisers-display]456[/advertisers-display] This is a test text area, filled with dummy text such as: Lorem ipsum dolor sit amet. [advertisers-display]789[/advertisers-display]";
$tag_start = "[advertisers-display]";
$tag_end = "[/advertisers-display]";
$this_array_get = getContents($thiss, $tag_start, $tag_end);
foreach($this_array_get as $this_get) {
if(isset($this_get[1])) {
echo "ID $this_get[0] | REPLACE THIS -> $this_get[1] WITH THIS -> [ REPLACED $this_get[0] ]<br />";
$thiss = str_replace($this_get[1], "[ REPLACED $this_get[0] ]", $thiss, $num);
}
}
echo "<br /> $thiss";
?>