1

So here comes the problem: I want to paste an advertisement after a specified ##th paragraph.

Let me show:

<p><img src="an/images/pic.jpg" /></p>
<p>Intro text</p>
<p>A paragraph</p>
<p>Another paragraph</p>

I want to paste the advertisement below the second paragraph. The final code should be like that:

<p><img src="an/images/pic.jpg /></p>
<p>Intro text</p>
<div>Yepp, let's make money</div>
<p>A paragraph</p>
<p>Another paragraph</p>

I tried some regexp but i don't get it. Ladies and Gentlemen please help me.

onetdev
  • 389
  • 2
  • 7
  • 17

3 Answers3

1

Taking RegEx match open tags except XHTML self-contained tags into consideration you might want to use a DOM/HTML parser for this.

<?php
$doc = new DOMDocument;
$doc->loadhtml( getHTML() );

$xpath = new DOMXPath($doc);
$ns = $xpath->query( '/html/body/p[2]' );
if ( 0 < $ns->length ) {
    $parent = $ns->item(0)->parentNode;
    $nextSibling = $ns->item(0)->nextSibling;

    $p = $doc->createElement('p', 'Hi, this is Scott coming to you from another place and time');
    $parent->insertBefore($p, $nextSibling);
}
echo $doc->savehtml();

function getHTML() {
return '<html><head><title>...</title><body>
<p><img src="an/images/pic.jpg" /></p>
<p>Intro text</p>
<p>A paragraph</p>
<p>Another paragraph</p>
</body></html>';
}

prints

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><title>...</title></head><body>
<p><img src="an/images/pic.jpg"></p>
<p>Intro text</p><p>Hi, this is Scott coming to you from another place and time</p>
<p>A paragraph</p>
<p>Another paragraph</p>
</body></html>
Community
  • 1
  • 1
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • Great solution. But... Is this solution faster and less resource demanding than Treffynnon's one? – onetdev Aug 31 '11 at 16:38
  • No, speed and footprint are not the "pitch" for this solution ;-) On the other hand it's not terribly slow either. Hard to tell without meticulous testing esp. when using a "runtime" like php. As usual it boils down to "is it fast enough; scalable enough; what does it offer; how robust but flexible is it?". And parsing arbitrary html only with regular expressions or a simple strpos/explode gets you into trouble sooner than later. (Both can be part of the parser but they are not a feasible parser on their own). – VolkerK Sep 01 '11 at 07:54
0

<p><img src="an/images/pic.jpg" /></p>

Don't forget to put an " at the end.

Egyébként Jó napot! :)

Akos
  • 1,997
  • 6
  • 27
  • 40
0

The following code should echo the advert after every fifth paragraph tag and after the the second paragraph. It should give you enough to modify and get it working.

It use two key functions:

The code:

$output = array();
$input = explode('<p>', $input);
foreach($input as $key => $line) {
    $output[] = '<p>' . $line;
    // every fifth paragraph
    if(($key % 5) === 0) {
        $output[] = '<div>Yepp, let\'s make money</div>';
    }
    // second paragraph
    if($key === 1) {
        $output[] = '<div>Yepp, let\'s make money</div>';
    }
}
echo implode('', $output);
Treffynnon
  • 21,365
  • 6
  • 65
  • 98