1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function interBlock($content, $ad=null, $element="</h3>", $afterElement=3, $minElements=5) { // block to be inserted here if (!$ad) $ad ='<h1>Your block here</h1>'; // split content $parts = explode($element, $content); // count element occurrences $count = count($parts); // check if the minimum required elements are found if(($count-1)<$minElements) return $content; $output=''; for($i=0; $i<$count; $i++) { // this is the core part that puts all the content together $output .= $parts[$i-1] . $element . (($i==$afterElement) ? $ad : ''); // this will insert after |
1 |
echo interBlock($my_content, $myAd, '</h2>', 4, 8); |
Insert block before HTML tag occurrence
And how about if you need to add the content, before the fourth title? Let’s do that. You will only have to change the tag to the HTML opening tag, change the order where all the content is printed.
1 |
$output .= (($i==$afterElement) ? $ad : '') .($i==1 ? $parts[0]:'') . $element . $parts[$i]; //this win insert before |
Insert block before or after an alternative tag
Now let’s imagine we want to insert an ad after the third title on the page. But if we don’t have enough titles on the page we just want to place an ad after the fourth paragraph. That is a reasonable scenario, isn’t it? So let’s modify our function. Instead of returning the content when the minimum number of elements is not found, we will just search for the $alternativeTag as we did in the first place.
1 2 3 4 5 6 7 |
// check if the minimum required elements are found if(($count-1)<$minElements) { $parts = explode($altElement, $content); // check for the alternative tag $count = count($parts); if(($count-1)<$minElements) return $content; // you can give up here and just return the original content $element = $altElement; // continue by using alternative tag instead of the primary one } |
And the final mixed function
This is how ower final PHP function will look like. You can use it to add content blocks after a certain number of HTML tag occurrences.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function interBlock($content, $ad=null, $element="<h3>", $altElement='<h3>', $afterElement=1, $minElements=5) { // Ser your block to be inserted here if (!$ad) $ad ='<h1>Your block here</h1>'; // split content $parts = explode($element, $content); // count element ocurrencies $count = count($parts); // check if the minimum required elements are found if(($count-1)<$minElements) { $parts = explode($altElement, $content); // check for the alternative tag $count = count($parts); if(($count-1)<$minElements) return $content; // you can give up here and just return the original content $element = $altElement; // continue by using alternative tag instead of the primary one } $output=''; for($i=1; $i<$count; $i++) { // this is the core part that puts all the content together //$output .= $parts[$i-1] . $element . (($i==$afterElement) ? $ad : ''); // this win insert after $output .= ($i==1 ? $parts[0]:'') . (($i==$afterElement) ? $ad : '') . $element . $parts[$i]; //this win insert before } return $output; } |
1 |
echo interBlock($my_content, $myAd, '<h2>', '<h3>', 4, 8); |
1 |
echo interBlock($my_content); |