一,第三方库的地址:
https://github.com/voku/simple_html_dom
二,安装库:simple_html_dom:
1,安装[lhdop@blog parsehtml]$ composer require voku/simple_html_dom
Info from https://repo.packagist.org: #StandWithUkraine
…
2,安装完成后查看安装成功的文件:
[lhdop@blog parsehtml]$ ls
composer.json composer.lock vendor
[lhdop@blog parsehtml]$ ls vendor/
autoload.php composer symfony voku
[lhdop@blog parsehtml]$ ls vendor/voku/
simple_html_dom
[lhdop@blog parsehtml]$ ls vendor/voku/simple_html_dom/
build CHANGELOG composer.json LICENSE README_API.md README.md src
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]
三,安装库时报错的处理:
1,报错:[lhdop@blog parsehtml]$ composer require voku/simple_html_dom
/usr/bin/env: ‘php’: No such file or directory
报错原因:composer没找到php
2,解决:把php添加到path中,
在/etc/profile中添加即可,例:
[root@blog composer]# vi /etc/profile
在末尾添加:
export PHP_HOME=/usr/local/soft/php7
export PATH=$PATH:/usr/local/bin:$PHP_HOME/bin
添加后使用效:
[root@blog composer]# source /etc/profile
四,使用simple_html_dom的例子
1,代码:<?php
//引用HtmlDomParser类
use voku\helper\HtmlDomParser;
//包含composer安装的目录下的autoload.php
require_once 'vendor/autoload.php';
//被解析的html代码
$one = '<img class="rich_pages wxw-img" data-backh="433" data-backw="578" data-cropselx1="0" data-cropselx2="578" data-cropsely1="1" data-cropsely2="1000" data-galleryid="" data-ratio="0.75" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/dr04j7dPGur6ansRPUjbwFtXwDRONx8yk7icBj5IdUOrUOMyhVrcANuuB0oYLWZ97nbWquUmR5fWU1a8KUGeZPA/640?wx_fmt=jpeg" data-type="png" data-w="1200" style="width: 100%;height: auto;">';
//解析
$dom = new HtmlDomParser($one);
$imgs = $dom->find('img');
//取第一个
$v = $imgs[0];
//得到属性
$attrsImg = $v->getAllAttributes();
var_dump($attrsImg);
?>
2,测试效果
[lhdop@blog parsehtml]$ /usr/local/soft/php7/bin/php parsedemo.php
array(14) {
["class"]=>
string(18) "rich_pages wxw-img"
["data-backh"]=>
string(3) "433"
["data-backw"]=>
string(3) "578"
["data-cropselx1"]=>
string(1) "0"
["data-cropselx2"]=>
string(3) "578"
["data-cropsely1"]=>
string(1) "1"
["data-cropsely2"]=>
string(4) "1000"
["data-galleryid"]=>
string(0) ""
["data-ratio"]=>
string(4) "0.75"
["data-s"]=>
string(7) "300,640"
["data-src"]=>
string(135) "https://mmbiz.qpic.cn/mmbiz_jpg/dr04j7dPGur6ansRPUjbwFtXwDRONx8yk7icBj5IdUOrUOMyhVrcANuuB0oYLWZ97nbWquUmR5fWU1a8KUGeZPA/640?wx_fmt=jpeg"
["data-type"]=>
string(3) "png"
["data-w"]=>
string(4) "1200"
["style"]=>
string(25) "width: 100%;height: auto;"
}
五,查看php的版本
[lhdop@blog ~]$ /usr/local/soft/php7/bin/php --version
PHP 7.4.2 (cli) (built: Apr 20 2022 16:49:58) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
标签:string,dom,blog,html,php,data
From: https://www.cnblogs.com/architectforest/p/17415239.html