
正文
php怎么获取标签数据 php的标签
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
PHP怎么获取表单提交的数据啊?
一、用file_get_contents以get方式获取内容,需要输入内容为:
1、?php
2、$url='';
3、$html=file_get_contents($url);
4、echo$html;
5、?
二、用file_get_contents函数,以post方式获取url,需要输入内容为
1、?php
2、$url='';
3、$data=array('foo'='bar');
4、$data=http_build_query($data);
5、$opts=array(
6、'http'=array(
7、 'method'='POST',
8、 'header'="Content-type:application/x-www-form-urlencoded\r\n".
9、 "Content-Length:".strlen($data)."\r\n",
10、 'content'=$data
11、)
12、);
13、$ctx=stream_context_create($opts);
14、$html=@file_get_contents($url,'',$ctx);
15、?
三、用fopen打开url,以get方式获取内容,需要输入内容为
1、?php
2、$fp=fopen($url,'r');
3、$header=stream_get_meta_data($fp);//获取信息
4、while(!feof($fp)){
5、$result.=fgets($fp,1024);
6、}
7、echo"urlheader:{$header}br":
8、echo"urlbody:$result";
9、fclose($fp);
10、?
四、用fopen打开url,以post方式获取内容,需要输入内容为
1、?php
2、$data=array('foo2'='bar2','foo3'='bar3');
3、$data=http_build_query($data);
4、$opts=array(
5、'http'=array(
6、'method'='POST',
7、'header'="Content-type:application/x-www-form-urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n".
8、"Content-Length:".strlen($data)."\r\n",
9、'content'=$data
10、)
11、);
12、$context=stream_context_create($opts);
13、$html=fopen(';id2=i4','rb',false,$context);
14、$w=fread($html,1024);
15、echo$w;
16、?
五、用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,需要输入内容为
1、?php
2、functionget_url($url,$cookie=false)
3、{
4、$url=parse_url($url);
5、$query=$url[path]."?".$url[query];
6、echo"Query:".$query;
7、$fp=fsockopen($url[host],$url[port]?$url[port]:80,$errno,$errstr,30);
8、if(!$fp){
9、returnfalse;
10、}else{
11、$request="GET$queryHTTP/1.1\r\n";
12、$request.="Host:$url[host]\r\n";
13、$request.="Connection:Close\r\n";
14、if($cookie)$request.="Cookie: $cookie\n";
15、$request.="\r\n";
16、fwrite($fp,$request);
17、while(!@feof($fp)){
18、$result.=@fgets($fp,1024);
19、}
20、fclose($fp);
21、return$result;
22、}
23、}
24、//获取url的html部分,去掉header
25、functionGetUrlHTML($url,$cookie=false)
26、{
27、$rowdata=get_url($url,$cookie);
28、if($rowdata)
29、{
30、$body=stristr($rowdata,"\r\n\r\n");
31、$body=substr($body,4,strlen($body));
32、return$body;
33、}
34、 returnfalse;
35、}
36、?
参考资料:
php-file_get_contents
相关问答
Q1: php如何获得页面某个标签的值
PHP想要获取表单的值,只能通过GET提交或者POST提交。
或者用js获取值做异步提交到本页面。
然后再用PHP进行接收。最基本的表单提交,用php接受值。
?php
if (isset($_GET['button1'])) {
echo $_GET['button1'];
}
?
!doctype html
html lang="en"
head
meta charset="UTF-8"
titleDocument/title
/head
body
form action=""
input type="text" name="button1" value="123"
input type="submit" value="提交"
/form
/body
/html
Q2: php用正则获取html标签内容
推荐使用querylist
?php
header("Content-type:text/html;charset=utf-8");
require 'QueryList/QueryList.class.php';
$url = "要抓取的网站";
$reg = array(
"title" = array("a","text"),
"src" = array("a","href"),
);
//$rang = "[id^=post-]";
$hj = QueryList::Query($url,$reg);
print_r($hj-jsonArr);
Q3: 求教:php读取html标签中内容,插入到数据库
php可以使用 fopen 打开文件 然后fread读文件。或者你可以使用file_get_contents获取文件内容。
然后连接数据库,把或者的内容,当成一个字段插入到数据库对应的字段里
Q4: php 如何取得select标签的值
一般select的值都是需要通过表单get或POST提交给其他程序页。其他程序页用$_post[“select的name”],这样就可以接收了。接收到的是一个数组。
Q5: 怎么用php正则获得a标签内的文字啊
php中正则匹配只要使用这两个函数:
preg_match_all
preg_match
这里使用preg_match_all,代码如下:
$subject = 'a href="xxx.php"abc测试/a'; // 假设这是需要匹配的字符串
$pattern = '/a href="[^"]*"[^]*(.*)\/a/'; // 这是匹配的正则表达式
preg_match_all($pattern, $subject, $matches); // 开始匹配,该函数会把匹配结果放入 $matches数组中
echo "pre";
print_r($matches);
/**
结果是:
Array
(
[0] = Array
(
[0] = abc测试
)
[1] = Array
(
[0] = abc测试
)
)
*/
php怎么获取标签数据的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php的标签、php怎么获取标签数据的信息别忘了在本站进行查找喔。






