Analyze HTML and save the result to an array. Let's take a look at the annotations. :)

Source: Internet
Author: User

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.

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.