PHP version of getElementById

Source: Internet
Author: User

<?php/** * Created by Phpstorm. * user:c.yu * date:14-8-2 * Time: PM 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]; Start label $end _tag = ' </'. $res [1]. ' > ';        End tag $lastStart _pos = Strrpos ($content, $start _tag);                $lastEnd _pos = Strrpos ($content, $end _tag);        Stores all start and end label 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; }//start tag number is not equal to the number of end tags, exit if (count ($STARTARR)! = Count ($ENDARR)) {exit (' error when calling getElementById:: &lt Font color= "Red" > Your label '. Htmlspecialchars ("{$start _tag} id= ' {$id} ' >").        ' There is no closure at the time of use, does not conform to XHTML, the system forcibly stops matching </font>. ');        }//does not match the start tag if (count ($STARTARR) <=0) {return '; }//If the start tag is full-text only one if (count ($STARTARR) ==1) {$secondary = substr ($content, $STARTARR [0], $ENDARR [0] + strlen            ($end _tag)-$startArr [0]);        return $secondary;        }//the index of the start tag of the specified ID in the set of all start tags $startIndex = 0;        $startCount = 1;                 for ($i = 0; $i < count ($STARTARR); $i + +) {if ($startArr [$i] = = = $start) {$startIndex = $i;            Break  }//if the subscript for the start label of the specified ID is the last if of the array ($startIndex = = = Count ($STARTARR)-1) {$secondary = substr ($content,      $STARTARR [$startIndex], $ENDARR [$startIndex] + strlen ($end _tag)-$startArr [$startIndex]);      return $secondary;   }//Specifies the ID start label for the next start label of the POS and the next start label between POS,//To be able to pair, take the end tag of the pos//<start1><end1><start2> The number of end between the Start1 and the next start2 is the number of 1,start, which is also 1, taking End1 pos//<start1><start2><end2><end1><        The number of end between Start3> Start1 and Start3 is the number of 2,start is also 2, take 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 version of getElementById

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.