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;
}

  

时间: 2024-07-31 16:45:42

Php版本的GetElementById的相关文章

兼容ie[6-9]、火狐、Chrome、opera、maxthon3、360浏览器的js本地图片预览

html代码: <div id="divPreview"> <img id="imgHeadPhoto" src="Images/Headphoto/noperson.jpg" style="width: 160px; height: 170px;border: solid 1px #d2e2e2;" alt="" /> </div> <asp:FileUpload

Jcrop图片剪切实例

一.创建一个弹出窗 1.html代码 <div id="BgDiv"></div> <div id="imgEditArea" style="display:none;top:0;"> <h2><fmt:message key="productDetail.lbl.editImg"/><a href="#" id="btnClose

整理项目中用到的angularjs及其他js代码

列表页数据展示 <!-- 查询结果 --> <ul id="ulView"> <li class="bgfff mt-15 ptb15 shopItem" ng-repeat="views in DataListView"> <input type="text" hidden value="{{ views.ShopCartID }}" class="Sh

iframe父子页面通信

一.同域下父子页面的通信 1.父页面调用子iframe页面 (1)通过iframe的Id获取子页面的dom,然后通过内置属性contentWindow取得子窗口的window对象,此方法兼容各个浏览器 document.getElementById('iframe_Id').contentWindow // contentWindow 不能省略 (2)通过iframe的name直接获取子窗口的window对象 iframe_Name.window    //window可以省略 (3)通过win

【JavaScript】图片上传预览

上传文件实时显示[一张图片]: 个人理解:给img的src传值:这个值就是input[type='file']的value: 不过你要判断浏览器类型[很多]:IE6.0,IE7/8/9,Fixfox7.0以下,Firefox7.0+ ,Chrome..... 直接贴代码[网上找的,记不得是哪位大神写的了] function PreviewImage(obj, imgPreviewId, divPreviewId) { var allowExtention = ".jpg,.bmp,.gif,.p

js本地图片预览代码兼容所有浏览器

html代码 <div id="divPreview" style="width: 160px; height: 170px"><img id="imgHeadPhoto" src="www.niyuewo.com" style="width: 160px; height: 170px;border: solid 1px #d2e2e2;" /> </div> <a

js本地图片预览,兼容ie[6-9]、火狐、Chrome17+、Opera11+、Maxthon3

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title></title> </head> <body> <div id="divPrevie

来之不易的js图片上传预览代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getS

php做图片上传功能

今天来做一个图片上传功能的插件,首先做一个html文件:text.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head&g