phphtmldom
❶ php主要应用那些方面
1、服务端脚本。
这是 PHP 最传统,也是最主要的目标领域。开展这项工作需要具备以下三点:
A、PHP 解析器(CGI 或者服务器模块)、
B、web 服务器、
C、web 浏览器。
需要在运行 web 服务器时,安装并配置 PHP,
然后,可以用 web 浏览器来访问 PHP 程序的输出,即浏览服务端的 PHP 页面。
如果只是实验 PHP 编程,所有的这些都可以运行在自己家里的电脑中。
2、命令行脚本。
可以编写一段 PHP 脚本,并且不需要任何服务器或者浏览器来运行它。
通过这种方式,仅仅只需要 PHP 解析器来执行。
这种用法对于依赖 cron(Unix 或者 Linux 环境)
或者 Task Scheler(Windows 环境)的日常运行的脚本来说是理想的选择。
这些脚本也可以用来处理简单的文本。
3、编写桌面应用程序。
对于有着图形界面的桌面应用程序来说,PHP 或许不是一种最好的语言,
但是如果用户非常精通 PHP,并且希望在客户端应用程序中使用 PHP 的一些高级特性,
可以利用 PHP-GTK 来编写这些程序。用这种方法,还可以编写跨平台的应用程序。
PHP-GTK 是 PHP 的一个扩展,在通常发布的 PHP 包中并不包含它。
如果对 PHP-GTK 感兴趣,请访问其网站以获取更多信息。
❷ PHP的simple_html_dom的具体用法。详细一点,
这是一个 PHP5 的 HTML 文档解析器,示例代码:
// 创建一个html对象,这个抓的是谷歌的首页
$html = file_get_html('http://www.google.com/');
//找到对象中所有的img的src
foreach($html->find('img') as $element)
echo $element->src . '<br>';
//找到对象中所有的a的href
foreach($html->find('a') as $element)
echo $element->href . '<br>';
就是以前你采集的时候,先file_get_contents把目标url的页面源码抓过来,假如要页面里的a标签的href,你要用正则分析。现在有了这个simple_html_dom。直接可以帮你分析出你需要的a标签里的href。
❸ php使用 simple html dom怎么修改html
用法示例:
<?php
// example of how to use basic selector to retrieve HTML contents
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://www.google.com/');
// find all link
foreach($html->find('a') as $e)
echo $e->href . '<br>';
// find all image
foreach($html->find('img') as $e)
echo $e->src . '<br>';
// find all image with full tag
foreach($html->find('img') as $e)
echo $e->outertext . '<br>';
// find all div tags with id=gbar
foreach($html->find('div#gbar') as $e)
echo $e->innertext . '<br>';
// find all span tags with class=gb1
foreach($html->find('span.gb1') as $e)
echo $e->outertext . '<br>';
// find all td tags with attribite align=center
foreach($html->find('td[align=center]') as $e)
echo $e->innertext . '<br>';
// extract text from table
echo $html->find('td[align="center"]', 1)->plaintext.'<br><hr>';
// extract text from HTML
echo $html->plaintext;
?>
❹ php中怎么利用dom查找到某元素的节点(<div class='aaa')并修改其元素内容
其实和客户端查找某元素的节点是差不多的,只是这里面的代码写在PHP中。
在JS中,如果只是获取class='aaa'的DIV元素,那么只要遍历文档树中的DIV元素,如果要获取class='aaa'的元素,不单是DIV,那么就要遍历整个文档树。我这里就只遍历DIV元素,取出class为'aaa'的DIV元素。且看下面的代码:
-------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>获取class为aaa的DIV元素并使之颜色为红</title>
</head>
<body>
<div class="aaa">第一个DIV</div>
<div class="aaa">第二个DIV</div>
<div>第二个DIV,但没有class</div>
<span class="aaa">span标签</span>
<script type="text/javascript">
//获取根节点
var root = document;
//获取DOM中的div元素,数组形式
var need = root.getElementsByTagName('div');
//遍历这个组数
for(var i=0;i<need.length;i++){
//如果这个组数元素(即该DIV)的className为'aaa',给它一个样式为前景红色
if(need[i].className=='aaa'){
need[i].style.color = 'red';
}
}
</script>
</body>
</html>
------------------------------------
执行后,只有class为'aaa'的DIV元素中的文本变成了红色
接下来就把这段JS脚本写到PHP里
--------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>获取class为aaa的DIV元素并使之颜色为红</title>
</head>
<body>
<div class="aaa">第一个DIV</div>
<div class="aaa">第二个DIV</div>
<div>第二个DIV,但没有class</div>
<span class="aaa">span标签</span>
<?php
echo "<script type=\"text/javascript\">
var root = document;
var need = root.getElementsByTagName('div');
for(var i=0;i<need.length;i++){
if(need[i].className=='aaa'){
need[i].style.color = 'red';
}
}
</script>";
?>
</body>
</html>
----------------------------------
就是一个echo这么简单,把JS脚本显示出来