0

I have a $text string with formatted html inside:

<h2>heading 1</h2>
<p>something</p>
<h2>heading 2</h2>
<p>something</p>
<h2>heading 3</h2>
<p>something</p>

I want to add an the string $ad after the fourth occurrence of a closing p tag.

I found out I need maybe strpos and str_replace functions.

I tried the following, but it doesn't work:

$explode = explode('</p>', $text,4);
$text = $explode[0].$ad.$explode[1];

Any hints for a beginner? Thanks!

  • You told explode to give you a maximum of four parts - but you are still trying to glue your $ad between the first and second of those parts. And the third and fourth part you don't even append any more. Plus, the `` are lost completely - you exploded at ``, so that is not part of the parts any more - you have to glue it back into the result yourself. – CBroe Sep 23 '21 at 14:31
  • Sorry, I wasn't clear. I know the code isn't working like that, it was just an example for my approach. My point was, that explode isn't working in general due to the problem, that I don't know how many parts of I have in my $text. And I think there are much more smart solutions out there! – coding_newbie Sep 23 '21 at 15:11

2 Answers2

1

It is best idea to use Regular expressions on this type of case. But, as you are using explode(), and also want a beginner friendly solution, let me help you with explode().

Your Mistakes:

  1. You are using </p> as separator, but it will be lost after explode. So your output $explode wont have these </p> tags.
  2. </p> will not work as a separator here, since your $text contains multiple lines and whitespaces between tags. So, between every </p> and <h2> there is some whitespaces and line-breaks.
  3. If this data/contents generates dynamically, you won't be able to know how many elements you will have after explode(). So using index number like $explode[1] won't help.

Let's talk about solutions:

// Formatted string in $text variable
$text = 
"<h2>heading 1</h2>
<p>something</p>
<h2>heading 2</h2>
<p>something</p>
<h2>heading 3</h2>
<p>something</p>
<h2>heading 4</h2>
<p>something</p>
<h2>heading 5</h2>
<p>something</p>
<h2>heading 6</h2>
<p>something</p>
<h2>heading 7</h2>
<p>something</p>
<h2>heading 8</h2>";

// Ad text, will be added after every 4th occurrence of </p>
$ad = "<p>This is an ad</p>";

// Add a | before <h2> tag, this will be our separator on explode
$string = str_replace("<h2", "|<h2", $text);

// this explode will give us an array consists of a h2 and a paragraph as elements.
$text = explode("|", $string);

// Our first element of the array will be blank, cause our explode() replace the
// very first | with a blank array element. That's why we are removing the first 
// blank element using array_shift.
array_shift($text);

// $j is a counter variable, this will help us to count every 4th occurrence
$j = 0;

// declare a new array for storing final output
$newText = array();

// using loop, lets add the $ad text
for ($i=0; $i < count($text); $i++) { 
   
    $j++;
    if ($j == 4) {
        // if it is 4th occurrence, add the $ad text
        array_push($newText, $text[$i].$ad);
        
        // reset counter to 0.
        $j = 0;
    }
    else{
        array_push($newText, $text[$i]);
    }  

}

// Display the final output
print_r($newText);

Note: If you want only one ad text after the first four paragraph, then you don't need the counter variable $j, you can check occurrence with $i variable.

PS: It's much better if you use regex.

  • 1
    Thank you, this helps really well! Any hint for the right regex approach if this is much better? – coding_newbie Sep 24 '21 at 04:22
  • 1
    Happy to help. As there are some existing answers on stack overflow, let me give you the link of those question (& answers) https://stackoverflow.com/questions/52306320/php-insert-html-after-the-nth-occurrence-of-a-p-tag –  Sep 24 '21 at 06:52
  • 1
    Here is another one. https://stackoverflow.com/questions/62173181/add-text-before-second-occurrence-of-p-in-a-string –  Sep 24 '21 at 06:53
1
<?php

$text = "
<h2>heading 1</h2>
<p>something</p>
<h2>heading 2</h2>
<p>something</p>
<h2>heading 3</h2>
<p>something</p>
<h2>heading 4</h2>
<p>something </p>
<h2>heading 5</h2>
<p>something</p>";

$ad = "<p>AD</p>";

//Divide the text into an array separating by the tag </p>
$explode = explode('</p>', $text);
//Insert AD after fourth occurrence
array_splice( $explode, 4, 0, array($ad) );
//Joins array elements back into text
$text = implode(" ",$explode);

echo $text;