Introduction: This is to analyze HTML and save the result to an array. Let's take a look at the annotations. :) On the details page, we will introduce PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 323742 'rolling = 'no'>
<? PHP
/*
* Parsehtml. php
* Author: Carlos Costa jordao
* Email: carlosjordao@yahoo.com
*
* My notation of variables:
* I _ = integer, EX: I _count
* A _ = array, a_html
* B _ = Boolean,
* S _ = string
*
* What it does:
*-Parses a HTML string and get the tags
*-Exceptions: HTML tags like <br> <HR> </a>, etc
*-At the end, the array will look like this:
* ["IMG"] [0] ["src"] = "XXX"
* ["IMG"] [1] ["src"] = "XXX"
* ["IMG"] [1] ["Alt"] = "XXX"
* ["A"] [0] ["href"] = "XXX"
*
*/
Function parsehtml ($ s_str)
{
$ I _indicatorl = 0;
$ I _indicatorr = 0;
$ S_tagoption = "";
$ I _arraycounter = 0;
$ A_html = array ();
// Search for a tag in string
While (is_int ($ I _indicatorl = strpos ($ s_str, "<", $ I _indicatorr )))){
// Get everything into tag...
$ I _indicatorl ++;
$ I _indicatorr = strpos ($ s_str, ">", $ I _indicatorl );
$ S_temp = substr ($ s_str, $ I _indicatorl, ($ I _indicatorr-$ I _indicatorl ));
$ A_tag = explode ('', $ s_temp );
// Here we get the tag's name
List (, $ s_tagname,) = each ($ a_tag );
$ S_tagname = strtoupper ($ s_tagname );
// Well, I am not interesting in <br>, </font> or anything else like that...
// So, this is false for tags without options.
$ B _booloptions = is_array ($ s_tagoption = each ($ a_tag) & $ s_tagoption [1];
If ($ B _booloptions ){
// Without this, We will mess up the Array
$ I _arraycounter = (INT) count ($ a_html [$ s_tagname]);
// Get the tag options, like src = "htt: //". Here, s_tagtokoption is 'src'
And s_tagtokvalue is '"http ://"'
Do {
$ S_tagtokoption = strtoupper (strtok ($ s_tagoption [1], "= "));
$ S_tagtokvalue = trim (strtok ("= "));
$ A_html [$ s_tagname] [$ I _arraycounter] [$ s_tagtokoption] =
$ S_tagtokvalue;
$ B _booloptions = is_array ($ s_tagoption = each ($ a_tag )))&&
$ S_tagoption [1];
} While ($ B _booloptions );
}
}
Return $ a_html;
}
?>
"Analyze HTML and save the result to an array. Let's take a look at the annotations. :) "More related articles
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/323742.html pageno: 15.