通過php提取HTML標籤

來源:互聯網
上載者:User
通過php提取HTML標籤

<?php/********************************** * 函數: tags* 功能: 從檔案中提取HTML標籤* * 入口:* $filename 檔案名稱* $tag   標籤名 * 返回:* 數組,每項為:*  tagName  String*  Text    String*  Attrs   Array* * 樣本:* PRint_r(tags("test1.htm","a"));* print_r("http://localhost/index.htm","img");* */function tags($filename,$tag) { $buffer = join("",file($filename)); $buffer = eregi_replace("\r\n","",$buffer); $tagkey = sql_regcase($tag); $buffer = eregi_replace("<$tagkey ","\n<$tag ",$buffer); $ar = split("\n",$buffer); foreach($ar as $v) {  if(! eregi("<$tagkey ",$v)) continue;  eregi("<$tagkey ([^>]*)((.*)</$tagkey)?",$v,$regs);  $p[tagName] = strtoupper($tag);  if($regs[3])   $p[Text] = $regs[3];  $s = trim(eregi_replace("[ \t]+"," ",$regs[1]))." ";  $s = eregi_replace(" *= *","=",$s);  $a = split(" ",$s);  for($i=0;$i<count($a);$i++) {   $ch = array();   if(eregi("=[\"']",$a[$i])) {    $j = $i+1;    while(!eregi("[\"']$",$a[$i])) {     $a[$i] .= " ".$a[$j];     unset($a[$j]);    }   }  }  foreach($a as $k) {   $name = strtoupper(strtok($k,"="));   $value = strtok("\0");   if(eregi("^[\"']",$value))    $value = substr($value,1,-1);   if($name)    $p[Attrs][$name] = $value;  }  $pp[] = $p; } return $pp;}?>

相關文章:

phpRegex提取html標籤的問題

提取html標籤的php程式碼範例

php Regex提取圖片url程式

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.