首页 > 代码库 > Php版本的GetElementById

Php版本的GetElementById

<?php/** * Created by PhpStorm. * User: c.yu * Date: 14-8-2 * Time: 下午11:11 */function GetElementById($content, $id, $return_type = ‘substr‘){    if (preg_match(‘@<([a-z]+)[^>]*id=[\"\‘]?‘ . $id . ‘[\"\‘]?[^>]*>@i‘, $content, $res)) {        $start = strpos($content, $res[0]);        $end = 0;        $start_tag = ‘<‘ . $res[1]; // 开始标签        $end_tag = ‘</‘ . $res[1] . ‘>‘; // 结束标签        $lastStart_pos = strrpos($content, $start_tag);        $lastEnd_pos = strrpos($content, $end_tag);                //储存所有开始标签和结束标签的pos        $startArr = array();        $endArr = array();           $next_pos = 0;        while ($next_pos < $lastStart_pos) {            $p_start = stripos($content, $start_tag, $next_pos);            $startArr[] = $p_start;            $next_pos = $p_start + 1;        }        $next_pos = 0;        while ($next_pos < $lastEnd_pos) {            $p_end = stripos($content, $end_tag, $next_pos);            $endArr[] = $p_end;            $next_pos = $p_end + 1;        }        //开始标签数量不等于结束标签数量,退出        if (count($startArr) != count($endArr)) {            exit(‘调用getElementById时出现错误::<font color="red">您的标签‘ . htmlspecialchars("{$start_tag} id=‘{$id}‘>") . ‘ 在使用时根本没有闭合,不符合xhtml,系统强制停止匹配</font>.‘);        }	//匹配不到开始标签        if(count($startArr)<=0){            return ‘‘;        }	//如果开始标签全文只有一个        if(count($startArr)==1){            $secondary = substr($content, $startArr[0], $endArr[0] + strlen($end_tag) -$startArr[0]);            return $secondary;        }	//在所有开始标签下标的集合中找到指定id的开始标签的下标        $startIndex = 0;        $startCount = 1;        for ($i = 0; $i < count($startArr); $i++) {            if ($startArr[$i] === $start) {                $startIndex = $i;                break;            }        }	//如果指定id的开始标签的下标是数组的最后一个        if($startIndex === count($startArr)-1){            $secondary = substr($content, $startArr[$startIndex], $endArr[$startIndex] + strlen($end_tag) -$startArr[$startIndex]);            return $secondary;        }	//指定id开始标签的下一个开始标签的pos和下一个开始标签的pos之间,	//能够配对,取结束标签的pos	//<start1><end1><start2>   start1和下一个start2之间end的数量是1,start的数量也是1,取end1的pos	//<start1><start2><end2><end1><start3>  start1和start3之间end的数量是2,start的数量也是2,取end1的pos        $startNext = $startArr[$startIndex + $startCount];	        $endCount = getEndCount($start, $startNext, $endArr,$startCount,$end);	        while ($endCount < $startCount) {            $startCount++;            $startNext = $startArr[$startIndex + $startCount];            $endCount = getEndCount($start, $startNext, $endArr,$startCount,$end);        }        $secondary = substr($content, $start, $end + strlen($end_tag) -$start);        return $secondary;    } else {        return false;    }}function getEndCount($start, $startNext, $endArr,$startCount,&$endPos){    $endCount = 0;    for ($j = 0; $j < count($endArr) - 1; $j++) {        if ($endArr[$j] < $startNext && $endArr[$j] > $start) {            $endCount++;            if($endCount === $startCount){                $endPos  =$endArr[$j];            }        }    }    return $endCount;}

  

Php版本的GetElementById