Home >Backend Development >PHP Tutorial >Extract HTML tags_PHP tutorial

Extract HTML tags_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:07:45980browse

/*******************************
*
* Author: Xu Zuning (Nagging)
* Email: czjsz_ah@stats.gov.cn
* Development: 2002.07
*
*
* Function: tags
* Function: Extract HTML tags
*
from the file * Entry:
* $filename File name
* $tag Tag name
* Return:
* Array, each item is:
* tagName String
* Text String
* Attrs Array
*
* Example:
* print_r(tags("test1.htm","a"));
* print_r("http://localhost/index.htm" ,"img");
*
*/

function tags($filename,$tag) {
  $buffer = join("",file($filename));
  $buffer = eregi_replace("rn","",$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 ([^>]*)((.*)    $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      $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("");
      if(eregi("^["']",$value))
        $value = substr($value,1,-1);
      if($name)
        $p[Attrs][$name] = $value;
    }
    $pp[] = $p;
  }
  return $pp;
}

?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/315020.htmlTechArticle?php /********************************* * *作者: 徐祖宁 (唠叨) *邮箱: czjsz_ah@stats.gov.cn *开发: 2002.07 * * *函数: tags *功能: 从文件中提取HTML标签 * *入口...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn