1

Possible Duplicate:
How to parse and process HTML with PHP?

$content = "
<p>This is the first paragraph</p>
<p>This is the second paragraph</p>
<p>This is the third paragraph</p>";

Given a string of html content as above, I need to do an insert after the N'th paragraph tag.

How can I parse the content and do an insert of a given string of text, say 'hello world' after paragraph 2?

Community
  • 1
  • 1
RegEdit
  • 2,134
  • 10
  • 37
  • 63
  • 1
    [Please don't use a regex to parse HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#answer-1732454)!! – Joseph Silber Jan 22 '12 at 19:17
  • @JosephSilber: I would politely request that you stop posting that everywhere. It's [neither factually correct](http://stackoverflow.com/a/4234491/345031) nor very [informative](http://meta.stackexchange.com/a/73168/148103), and people obviously don't read past the off-topic joke. – mario Jan 22 '12 at 19:26

3 Answers3

7

You could use PHP explode and implode functions. Here's a concept:

$content = "
<p>This is the first paragraph</p>
<p>This is the second paragraph</p>
<p>This is the third paragraph</p>";

$content_table = explode("<p>", $content);

This will create $content_table with values:

Array ( [0] => [1] => This is the first paragraph
[2] => This is the second paragraph
[3] => This is the third paragraph
) 

Now you can change whatever you want to, using $content_table[2] for paragraph 2. For example you can do:

$content_table[2] .= "hello world!";

When you're done, just implode the table to string again:

$content = implode($content_table, "<p>");
beam022
  • 1,793
  • 4
  • 20
  • 27
6

If you are sure about the HTML structure of your string, you can count the seen paragraphs in the callback's static variable.

$content = preg_replace_callback('#(<p>.*?</p>)#', 'callback_func', $content);

function callback_func($matches)
{
  static $count = 0;
  $ret = $matches[1];
  if (++$count == 2)
    $ret .= "<p> Additional paragraph</p>";
  return $ret;
}

Note that this solution is not reentrant, it is only a concept.

Alan
  • 504
  • 2
  • 5
  • +1 for a solution vs commentary :) Looks pretty simple. Checking it out now. – RegEdit Jan 22 '12 at 19:55
  • 1
    Would the answer be faster or slower then the explode|implode answer below? – Banago Mar 17 '16 at 17:06
  • Just with preg_replace (no need of callback): `$content = preg_replace('#((?:\[\s]|.*?){2})(\)#im', '$1additional text', $content, 1, $count);` will add additional text behind the third paragraph. Increase the number inside {} to skip more paragraphs; to skip n paragraphs set it to n-1; {1} will add text after paragraph two, as asked in the question. – LarS Jun 12 '19 at 20:53
-3

function can help str_replace()

http://php.net/manual/es/function.str-replace.php

<? str_replace('<p>This is the second paragraph</p>','<p>This is the second paragraph</p> hello world', $content);?>
del_dan
  • 873
  • 1
  • 9
  • 16
  • @HoeHoeHoe, this code does what you ask, maybe I not understood – del_dan Jan 23 '12 at 12:17
  • @del_dan What if second paragraph could be any other string? Seems like you assumed that it will always be _This is the second paragraph_, which (I think) wasn't RegEdit's intention. – beam022 Jan 31 '12 at 21:01
  • truth is not the best solution. only one solution. – del_dan Jan 31 '12 at 21:32