The problem is quite simple…you just want to count a specific tag in a page and insert some content. After or before it’s just a matter of perspective. So let’s say you want an ad, for example, to be inserted, after the fourth H2 tag.
So let’s go and insert an ad code or any other content block after some HTML tag. Here we have a simple PHP function:
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) { // 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) 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 win insert after |
…and you can simply call it like this:
1 | echo interBlock($my_content, $myAd, '</h2>', 4, 8); |
This will place, $myAd in $myContent , after the fourth occurrence of the closing tag </h2> if there are at least 8 occurrences of the mentioned tag. That was simple, was it? Now let’s get a little bit more complicated.
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 add after the third title in the page. But if we don’t have enough titles in 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 like 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 } |
At this point, you can just let the function to continue because you are using the alternative tag.
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; } |
….and this is the way you call it to insert a block before the fourth H2 tag. If you do not have 8 H2 it will use the H3 tag.
1 | echo interBlock($my_content, $myAd, '<h2>', '<h3>', 4, 8); |
Since almost all the params have a default value into the function definition, you can just adjust that to your needs and just call the function with the required $my_content var.
1 | echo interBlock($my_content); |
Note:
Of course, there are other scenarios and you can build a robust function or even class to achieve a more dynamic usage. For example, you can use an array of elements to split the content, you can add an if to switch between after and before depending on the tag structure, etc etc.
Feel free to do that, adapt it to your needs.