Download the PHP package keesiemeijer/insert-content without Composer
On this page you can find all versions of the php package keesiemeijer/insert-content. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download keesiemeijer/insert-content
More information about keesiemeijer/insert-content
Files in keesiemeijer/insert-content
Package insert-content
Short Description Insert content in strings with HTML
License GPL-2.0+
Homepage https://github.com/keesiemeijer/insert-content
Informations about the package 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 is used to find and add the content after paragraphs.
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
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.
Most functions (or scripts) that are similar to this one would wrongly add your content ater the second <p>
inside the <blockquote>
element.
- Set the
top_level_p_only
argument tofalse
to include nested paragraphs as well. - Use the
parent_element_id
argument to only include top-level paragraphs (nested) in an HTML element with a specific id.
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 to insert content after a number of paragraphs instead of in the middle. - Or use the
insert_every_p
argument to insert content after every number of paragraphs instead of in the middle.
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 to false to not append content if no (or not enough) paragraphs are found.
Inserted Content
The inserted content will be wrapped in a HTML paragraph element <p></p>
by default.
- Use the
insert_element
argument to wrap the inserted content in any other block-level HTML element. Change the element todiv
if you want to insert content containing block-level HTML tags.
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:
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 theinsert_every_p
argument below.$args
(array) Array with optional argumentsparent_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 frominsert_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
Example 1
Add content after the second paragraph in this content:
For this example pretend the above HTML content is stored inside the $content
variable.
The output for this example is this.
Example 2
Add content after the second paragraph in an HTML element with the id specific-id
.
For this example pretend the above HTML content is stored inside the $content
variable.
The output for this example is this.