本文目录一览:
1、怎样用PHP实现超链接?
2、php 截取一个网页的所有超链接的代码 急急
3、php 取出页面中所有的超链接
怎样用PHP实现超链接?
用户点击超链接可以做的有几种:
1、跳转到新页面,此时由于又发出了新的请求,所以会由服务器进行处理。但无法判断是用户点击了超链接,因为用户可直接通过地址栏输入跳转的地址。
2、运行Javascript,Javascript是客户端脚本,与PHP无关。可以通过Ajax动态加载数据,但并不是PHP点击链接后引用文件。两者是截然不同的两个概念。
超级链接
超级链接简单来讲,就是指按内容链接。
php 截取一个网页的所有超链接的代码 急急
?php
header("Content-type: text/html; charset=utf-8");
if(!empty($_POST['input_text'])) {
ini_set('default_socket_timeout', 60); //php file_get_contents超时控制
if(!$data = file_get_contents($_POST['input_text'])) {
echo "Time out!";
return false;
}else{
$charset_pos = stripos($data,'charset');
if($charset_pos) { //页面数据编码格式转换
if(stripos($data,'utf-8',$charset_pos)) {
$data = iconv('utf-8','utf-8',$data);
}else if(stripos($data,'gb2312',$charset_pos)) {
$data = iconv('gb2312','utf-8',$data);
}else if(stripos($data,'gbk',$charset_pos)) {
$data = iconv('gbk','utf-8',$data);
}
}
}
//获取超链接核心代码
$pattern = '/a(.*?)href="((http(s?):\/\/)?([^\"]+))"([^]*?)([^]*?)\/a/i';
preg_match_all($pattern, $data, $links);
$links[2]为全部链接。
$br = 5;
echo "tabletr";
//$links[2]为所有超链接组成的数组。
foreach($links[0] as $count = $link){
if($count!=0 $count%$br == 0) echo "/trtr";
echo "td".$link."/td";
}
echo "/tr/table";
die;
}else {
?
html
head
titleGet Web Page/title
meta http-equiv="Content-Type" cOntent="text/html; charset=utf-8" /
meta http-equiv="Content-Language" cOntent="zh-CN" /
script type="text/Javascript"
function createXMLHTTP()
{
try
{
var request = new XMLHttpRequest();
}
catch(e1)
{
var arrVersiOns= ["Microsoft.XMLHTTP","MSXML2.XMLHttp.4.0",
"MSXML2.XMLHttp.3.0","MSXML2.XMLHttp.5.0"];
for(var i=0;i arrVersions.length;i++){
try{
request = new ActiveXObject(arrVersions[i]);
}catch(e2){
request = false;
}
}
}
return request;
}
function ajax_post(url, params, target_id)
{
request = new createXMLHTTP();
request.Onreadystatechange= function() {
if (this.readyState == 4)
if (this.status == 200)
if (this.responseText != null)
document.getElementById(target_id).innerHTML = this.responseText;
}
request.open("POST", url, true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");
request.send(params);
}
var checked = false;
function check_(value) {
checked = value;
}
function get_key(event) {
event = event || window.event;
if(event.keyCode==13 checked != false)
{
var url = document.getElementById('input_text').value;
if(url != '') {
get_page();
}else {
document.getElementById('input_text').onfocus();
return false;
}
}
}
function get_page() {
var url = document.getElementById('input_text').value;
if(!url) {
return false;
}else {
if(document.getElementById('output_page').innerHTML != '') {
document.getElementById('output_page').innerHTML = '';
}
}
if(url.indexOf('
url = '
}
ajax_post(
'?php echo $_SERVER['PHP_SELF']; ?',
'input_text='+url,
'output_page'
);
document.getElementById('click_show').style.display = 'block';
document.getElementById('back_a').href = document.location.href;
document.getElementById('origin_website').href = url;
}
/script
style
.div_box{
margin-top:10px;
}
.input_box{
border:1px solid;
margin-left:10px;
margin-top:2px;
height:15px;
float:left;
size:32
font-size: 14px;
}
.button_box{
float:left;
height:23px;
padding-bottom:3px;
}
.hide_box{
display:none;
}
.a_box{
margin-left:10px;
margin-top:3px;
height:15px;
float:left;
font-size: 14px;
}
.clear_box{
height:50px;
}
/style
/head
body Onkeydown="get_key(event)"
div class="div_box"
input id="input_text" class="input_box" type="text" value="" Onclick="check_(true)" Onblur="check_(false)"/input
input type="button" class="button_box" Onclick="get_page()" value="Get it!" /input
div id="click_show" class="hide_box"
a id="origin_website" class="a_box" href="#" target="_black"访问原站/a
a id="back_a" class="a_box" href="#"后退/a
/div
/div
div class="clear_box"/div
div id="output_page"/div
/body
/html
?php
}
//End_php
php 取出页面中所有的超链接
正则:$pre = "/lispan class=\"txt\"a href=\"(.*?)\" title/";
每个遍历出来后再加上 。