How to insert line after instance x header tag?

First of all, thanks for helping me here, I really appreciate your time.

Let me tell you a little about what I'm trying to accomplish:

I am working on a small improvement to the site where I want to add a simple, related message widget in the content area of ​​certain messages.

Now, for any given message, I have all the contents of my message stored in a variable. Let me call it $ content.

What I hope to do is parse this $ content line and insert the widget right after the second instance of the H3 tag.

This is an example of one of my failed attempts:

$widgetizedContent = explode("</h3>", $content); $widgetizedContent[1] .= 'widget code'; $finalContent = implode($widgetizedContent, "<p>"); 

I'm not quite sure where to go from here, and I will be grateful for any direction that you could provide to me.

Edit: this is the output:

 <h3>This is my header text[this is the text I'm trying to insert]</h3> 

Instead of embedding the widget code after H3, it inserts it before the closing H3 tag. I don’t think that I understand that I will explode, but I also have to :)

+4
source share
1 answer

Based on your code, you can add text after the second </h3> by first deferring the text to the content after the tag (and not to the content).

 $widgetizedContent = explode("</h3>", $content); $widgetizedContent[2] = 'widget code'.$widgetizedContent[2] ; $finalContent = implode("</h3>", $widgetizedContent); 

Of course, this blindly suggests that there will be at least 2 </h3> tags that may or may not suit your site.

+1
source

Source: https://habr.com/ru/post/1433928/


All Articles