matejch/html_helpers

用于删除元素和内容,以及提取文件路径的辅助类

1.0.1 2022-06-17 10:29 UTC

This package is auto-updated.

Last update: 2024-09-17 17:56:17 UTC


README

latest_tag

两个PHP辅助类,用于删除HTML标签和提取文件路径

在您的项目中安装

composer require matejch/html_helpers "^1.0.0"

class HtmlTagRemover

使用HtmlTagRemover类,您可以从HTML字符串中删除指定的HTML标签及其内容

示例:根据名称删除标签

$service = new HtmlTagRemover($htmlString);
service->removeTags(['a','img']);

示例:根据属性内容删除标签

$service = new HtmlTagRemover($htmlString);

$service->removeTags(['a' => 'href[files]']);

/** remove 'a' tags where href attribute contains `files` substring and span elements contains class with `test` string */
$service->removeTags(['a' => 'href[files]','span' => 'class[test]']);

class HtmlFileExtractor

我创建了此类,因为我需要在发送邮件之前从HTML中提取文件路径,并将其作为附件添加

此类可以自动从imga标签中返回源代码

也可以从自定义标签或属性中返回

示例:从imga标签中获取文件路径

$service = new HtmlFileExtractor($htmlString);

/** return files as array of strings directly from href and src */
$service->getFiles(['img','a'])

示例:从其他标签或指定属性中获取文件路径

/** return files as array of strings from class attributes of span elements */
$service->getFiles(['span' => 'class'])

$service->getFiles(['span' => 'class','img'=>'alt'])

/** return files as array of strings from class attributes of span elements, 
 * where class contains substring 'es' and from alt attribute of image tags
 */
$service->getFiles(['span' => 'class[es]','img'=>'alt'])

从您的项目中移除软件包

composer remove matejch/html_helpers