PHPackages                             keesiemeijer/insert-content - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. keesiemeijer/insert-content

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

keesiemeijer/insert-content
===========================

Insert content in strings with HTML

2.0.0(8y ago)11612GPL-2.0+PHP

Since Apr 28Pushed 6y ago1 watchersCompare

[ Source](https://github.com/keesiemeijer/insert-content)[ Packagist](https://packagist.org/packages/keesiemeijer/insert-content)[ Docs](https://github.com/keesiemeijer/insert-content)[ RSS](/packages/keesiemeijer-insert-content/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (3)Used By (0)

Insert Content Between HTML Paragraphs [![Build Status](https://camo.githubusercontent.com/05a0ae744cc5896e3a963b37aca31ed219ec9486365a8f4e8c27e2175195b728/68747470733a2f2f7472617669732d63692e6f72672f6b65657369656d65696a65722f696e736572742d636f6e74656e742e7376673f6272616e63683d6d6173746572)](http://travis-ci.org/keesiemeijer/insert-content)
======================================================================================================================================================================================================================================================================================================================================================

[](#insert-content-between-html-paragraphs-)

PHP functions to insert content between or after HTML paragraphs the right way, ***without*** using regular expressions.

Insert content in a string containing HTML markup. The PHP [DOM module](https://secure.php.net/manual/en/book.dom.php) is used to find and add the content after paragraphs.

Features
--------

[](#features)

- Insert content after the middle paragraph (default)
- Insert content after a set number of paragraphs
- Insert content after every number of paragraphs
- Exclude nested paragraphs (default)
- Inserted content can contain HTML as well.

### Nested Paragraphs

[](#nested-paragraphs)

Only top-level (non nested) HTML paragraphs are used to add content after. Let's say you wanted to add content after the second paragraph in this content.

```
a div with content
first top-level paragraph

	This is a nested paragraph

second top-level paragraph

third top-level paragraph
```

Most functions (or scripts) that are similar to this one would wrongly add your content ater the second `` inside the `` element.

- Set the `top_level_p_only` [argument](https://github.com/keesiemeijer/insert-content#parameters) to `false` to include nested paragraphs as well.
- Use the `parent_element_id` [argument](https://github.com/keesiemeijer/insert-content#parameters) to only include top-level paragraphs (nested) in an HTML element with a specific id.

### Insert Position

[](#insert-position)

By default content is inserted after the middle paragraph. In other words, if the HTML content contains four paragraphs it will be inserted after the second.

- Use the `insert_after_p` [argument](https://github.com/keesiemeijer/insert-content#parameters) to insert content after a number of paragraphs instead of in the middle.
- Or use the `insert_every_p` [argument](https://github.com/keesiemeijer/insert-content#parameters) to insert content after every number of paragraphs instead of in the middle.

### No Paragraphs Found

[](#no-paragraphs-found)

The content you want to insert will be added at the end if no paragraphs were found. Equally so, if you've set it to insert content after a set number of paragraphs, and not enough paragraphs were found, it's inserted after the last found paragraph. If you're using the `parent_element_id` argument and no paragraphs are found in that element the content will be inserted after the parent element.

- Set the `insert_if_no_p` [argument](https://github.com/keesiemeijer/insert-content#parameters) to false to not append content if no (or not enough) paragraphs are found.

### Inserted Content

[](#inserted-content)

The inserted content will be wrapped in a HTML paragraph element `` by default.

- Use the `insert_element` [argument](https://github.com/keesiemeijer/insert-content#parameters) to wrap the inserted content in any other block-level HTML element. Change the element to `div` if you want to insert content containing block-level HTML tags.

Usage
-----

[](#usage)

Include the `insert-content.php` file in your project to make use of the `insert_content()` function. Or require it in your project with composer `require keesiemeijer/insert-content`

```

```

The defaults for the optional arguments ($args) are:

```
$args = array(
	'parent_element_id' => '',
	'insert_element'   => 'p',
	'insert_after_p'   => '',
	'insert_every_p'   => '',
	'insert_if_no_p'   => true,
	'top_level_p_only' => true,
);
```

### Parameters

[](#parameters)

- `$content` (string)(required) String of content (with paragraphs) you want to add content in between.
- `$insert_content` (string or array) String of content you want to insert. Use an array of content strings if you want to insert different content with the `insert_every_p` argument below.
- `$args` (array) Array with optional arguments
    - `parent_element_id` (string) Parent element id (without #) to search paragraphs in. Default: empty string. Search for paragraphs in the entire content.
    - `insert_element` (string) Block-level HTML element the inserted content (`$insert_content`) is wrapped in. Default: 'p'. (e.g. 'p', 'div', etc.)
    - `insert_after_p` (int) Insert content after a number of paragraphs. Default: empty string. The content is inserted after the middle paragraph.
    - `insert_every_p` (int) Insert content after every number of paragraphs. Default: empty string. The content is inserted after the middle paragraph.
    - `insert_if_no_p` (boolean) Insert content even if the required number of paragraphs from `insert_after_p` (above) are not found. Default: true. Insert after the last paragraph. (inserts after the content if no paragraphs are found).
    - `top_level_p_only` (boolean) Insert content after top level paragraphs only. Default: true (recommended)

Examples
--------

[](#examples)

### Example 1

[](#example-1)

Add content after the second paragraph in this content:

```
a div with content
first top-level paragraph

	This is a nested paragraph

second top-level paragraph
third top-level paragraph
```

For this example pretend the above HTML content is stored inside the `$content` variable.

```

```

The output for this example is this.

```
a div with content
first top-level paragraph

	This is a nested paragraph

second top-level paragraph
I was inserted after the second paragraph
third top-level paragraph
```

### Example 2

[](#example-2)

Add content after the second paragraph in an HTML element with the id `specific-id`.

```
first paragraph
second paragraph

	first top-level paragraph inside the targeted element
	second top-level paragraph inside the targeted element
	third top-level paragraph inside the targeted element

third paragraph
```

For this example pretend the above HTML content is stored inside the `$content` variable.

```

```

The output for this example is this.

```
first paragraph
second paragraph

	first top-level paragraph inside the targeted element
	second top-level paragraph inside the targeted element
	I was inserted after the second paragraph inside the targeted element
	third top-level paragraph inside the targeted element

third paragraph
```

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2984d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d222b9a661517627f6525f8302381004baa4ec585d2eb5c10f0e8b88fa2ee462?d=identicon)[keesiemeijer](/maintainers/keesiemeijer)

---

Top Contributors

[![keesiemeijer](https://avatars.githubusercontent.com/u/1436618?v=4)](https://github.com/keesiemeijer "keesiemeijer (44 commits)")

### Embed Badge

![Health badge](/badges/keesiemeijer-insert-content/health.svg)

```
[![Health](https://phpackages.com/badges/keesiemeijer-insert-content/health.svg)](https://phpackages.com/packages/keesiemeijer-insert-content)
```

###  Alternatives

[sculpin/blog-skeleton

A Skeleton for a Sculpin Based Blog

823.0k](/packages/sculpin-blog-skeleton)[andychukse/laravel-pricing-plans

A package provide pricing plans for Laravel.

112.5k](/packages/andychukse-laravel-pricing-plans)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
