I need to correct a string with wrong heading-tags and missing p-tags:
<h3>1. Title</h3>
Text
<h3>1.1 Subtitle</h3>
Text
<h3>1.2. Subtitle</h3>
Should get
<h2>1. Title</h2>
<p>Text</p>
<h3>1.1. Subtitle</h3>
<p>Text</p>
<h3>1.2. Subtitle</h3>
That means every heading of a first level of the list should be a h2-tag. The second level could have the format 1.1.
or 1.1
, which should be corrected with the missing .
If there is no tag at all, a p-tag should be added.
$lines = explode(PHP_EOL, $text);
foreach ($lines as $line) {
if(!strpos($line,"<h")) $line = '<p>'.$line.'</p>';
$output = $output.$line;
}
So this adds the missing p-tags, but I don't know how to take care of the heading tags and the optional missing point of the second level.