Skip to content
Advertisement

Use Regex to Split Paragraphs that are not wrapped in div or Table

I am trying to insert some text after every paragraph in my content.

I explode my content by </p> It is done using following code:

JavaScript

Now my $Content looks like:

JavaScript

I want to split if <p> isn’t wrapped inside <div> or <table> of anything else.

You can say that the </p> should have a <p> after it.

I read Regex can be helpful in achieveing it.

Here’s the basic regex I built:

JavaScript

This regex currently removes the

itself from the array, it keeps content inside p but not the

itself, and it doesn’t check for it being either having a

before it or

after it.

Advertisement

Answer

If i understood your problem you have a string with tags such as:

JavaScript

And you want to add another element right after each p that is not contained in any other element. And you want to do that purely through PHP, correct ?

In my opinion your best option is using DOMDocument.

Take a look at the solution below:

JavaScript

Basically i am taking your string converting it into an HTML DOM then i iterate through all the p elements and if their parent is body then i create a document fragment which will append XML raw data to create your deeply nested structure without creating each element individually. Finnaly i insert the newly create fragment after each iterated p element.

The output will look something like this:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement