keesiemeijer/insert-content

使用HTML在字符串中插入内容

2.0.0 2018-04-28 15:22 UTC

This package is auto-updated.

Last update: 2024-09-21 20:43:44 UTC


README

PHP函数,用于以正确的方式在HTML段落之间或之后插入内容,无需使用正则表达式。

在包含HTML标记的字符串中插入内容。PHP DOM模块用于查找并添加段落后的内容。

功能

  • 在中间段落之后插入内容(默认)
  • 在设置数量的段落之后插入内容
  • 每隔一定数量的段落插入内容
  • 排除嵌套段落(默认)
  • 插入的内容可以包含HTML。

嵌套段落

仅使用顶级(非嵌套)HTML段落来添加内容之后。比如说,你想要在这个内容中的第二个段落之后添加内容。

<div>a div with content</div>
<p>first top-level paragraph</p>
<blockquote>
	<p>This is a nested paragraph</p><!-- not a top-level paragraph -->
</blockquote>
<p>second top-level paragraph</p>
<!-- Content should be inserted here -->
<p>third top-level paragraph</p>

大多数与此类似的功能(或脚本)会错误地在<blockquote>元素内的第二个<p>之后添加你的内容。

  • top_level_p_only 参数设置为false以包括嵌套段落。
  • 使用parent_element_id 参数以仅包括具有特定id的HTML元素中的顶级段落(嵌套)。

插入位置

默认情况下,内容在中间段落之后插入。换句话说,如果HTML内容包含四个段落,它将在第二个之后插入。

  • 使用insert_after_p 参数以在指定数量的段落之后插入内容,而不是在中间。
  • 或者使用insert_every_p 参数以在每隔一定数量的段落之后插入内容,而不是在中间。

未找到段落

如果未找到段落,则要插入的内容将添加到末尾。同样,如果你将其设置为在设置数量的段落之后插入内容,并且未找到足够的段落,它将在找到的最后一个段落之后插入。如果你使用parent_element_id参数,并且在该元素中未找到段落,则内容将在父元素之后插入。

  • insert_if_no_p 参数设置为false,以不在未找到(或未找到足够)段落的情况下附加内容。

插入内容

默认情况下,插入的内容将被包装在HTML段落元素<p></p>中。

  • 使用insert_element 参数将插入的内容包装在任何其他块级HTML元素中。如果你想插入包含块级HTML标签的内容,请将元素更改为div

用法

insert-content.php文件包含到您的项目中以使用insert_content()函数。或者,使用composer在项目中引入它require keesiemeijer/insert-content

<?php
require 'path/to/insert-content.php';

echo keesiemeijer\Insert_Content\insert_content( $content, $insert_content, $args ); 
?>

可选参数($args)的默认值如下

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

参数

  • $content(字符串)(必填)要插入内容的字符串(带有段落)。
  • $insert_content(字符串或数组)要插入的字符串。如果想要在下面的insert_every_p参数中使用不同内容,请使用内容字符串数组。
  • $args(数组)包含可选参数的数组
    • parent_element_id(字符串)要搜索段落的父元素id(不包含#)。默认:空字符串。在整个内容中搜索段落。
    • insert_element(字符串)插入内容($insert_content)所包裹的块级HTML元素。默认:'p'。(例如:'p'、'div'等)
    • insert_after_p(整数)在指定数量的段落之后插入内容。默认:空字符串。内容将在中间段落之后插入。
    • insert_every_p(整数)在指定数量的段落之后插入内容。默认:空字符串。内容将在中间段落之后插入。
    • insert_if_no_p(布尔值)即使没有找到insert_after_p(上方)中要求的段落数量,也要插入内容。默认:true。在最后一段落之后插入。(如果没有找到段落,则在内容之后插入)。
    • top_level_p_only(布尔值)仅在顶级段落之后插入内容。默认:true(推荐)

示例

示例 1

在此内容中添加到第二个段落之后

<div>a div with content</div>
<p>first top-level paragraph</p>
<blockquote>
	<p>This is a nested paragraph</p><!-- not a top-level paragraph -->
</blockquote>
<p>second top-level paragraph</p>
<p>third top-level paragraph</p>

对于此示例,假设上述HTML内容存储在$content变量中。

<?php
$args = array(
	'insert_after_p' => 2, // Insert after the second paragraph
);

// Content you want to insert (without the parent element HTML tag)
$insert_content = 'I was inserted after the <strong>second</strong> paragraph';

echo keesiemeijer\Insert_Content\insert_content( $content, $insert_content, $args );
?>

此示例的输出如下。

<div>a div with content</div>
<p>first top-level paragraph</p>
<blockquote>
	<p>This is a nested paragraph</p><!-- not a top-level paragraph -->
</blockquote>
<p>second top-level paragraph</p>
<p>I was inserted after the <strong>second</strong> paragraph</p>
<p>third top-level paragraph</p>

示例 2

在id为specific-id的HTML元素中添加到第二个段落之后。

<p>first paragraph</p>
<p>second paragraph</p>
<div id='specific-id'>
	<p>first top-level paragraph inside the targeted element</p>
	<p>second top-level paragraph inside the targeted element</p>
	<p>third top-level paragraph inside the targeted element</p>
</div>
<p>third paragraph</p>

对于此示例,假设上述HTML内容存储在$content变量中。

<?php
$args = array(
	'parent_element_id' => 'specific-id'
	'insert_after_p'    => 2, // Insert after the second paragraph
);

// Content you want to insert (without the parent element HTML tag)
$insert_content = 'I was inserted after the <strong>second</strong> paragraph inside the targeted element';

echo keesiemeijer\Insert_Content\insert_content( $content, $insert_content, $args );
?>

此示例的输出如下。

<p>first paragraph</p>
<p>second paragraph</p>
<div id='specific-id'>
	<p>first top-level paragraph inside the targeted element</p>
	<p>second top-level paragraph inside the targeted element</p>
	<p>I was inserted after the <strong>second</strong> paragraph inside the targeted element</p>
	<p>third top-level paragraph inside the targeted element</p>
</div>
<p>third paragraph</p>