home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / tidbits6.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  3.6 KB  |  92 lines

  1. Variable Functions in Real Life
  2.  
  3. In the last phpTidbit, you've taken a glance at PHP3's variable functions. This issue shows a real life example (which by the way could be very useful): Parsing HTML for certain tags. 
  4. The following function html_parse parses a file or string for a tag and returns its content and attributes to a custom callback function (you've learned what this means in the last phpTidbit). This is useful if you want for example extract all headings from a file, create a list of all links or use it for custom templates. You don't have to learn XML just for this! 
  5. Currently there are some limitations: 
  6.  
  7. - it handles only tags with an appropiate closing tag: <img>, <param> etc. won't work. 
  8. - attributes of the tag must be enclosed by double quotes (this could be changed easily though). 
  9.  
  10. The principle of this function is simple: 
  11. Every time it encounters the tag you specified as parameter, it calls your callback function. 
  12.  
  13. The syntax: 
  14.  
  15. html_parse($html_file, $element_handler, $tag): 
  16. - $html_file: either a string containing a valid filename or a string containing HTML markup. 
  17. - $element_handler: your callback-function (see below for the parameters this function must accept) 
  18. - $tag: the tag you want to search for. 
  19.  
  20. The following example extracts all <H2>-headings from "test.html" and displays them: 
  21.  
  22. function handler($attributes, $content) 
  23. while (list($key, $value) = @each($attributes)) 
  24. echo "$key: $value<br>"; 
  25. echo "$content<p>"; 
  26. html_parse("/usr/local/htdocs/test.html", "handler", "h2"); 
  27.  
  28. And here is the complete function: 
  29. /* 
  30. This function opens and parses $html_file for $tag 
  31. and returns its content and its attributes to the 
  32. callback function $element_handler. 
  33. $element_handler is a custom function which acts upon 
  34. the content and the attributes of $tag and gets called 
  35. everytime $tag is closed. It must accept the following parameters: 
  36. - $attributes (attributes of $tag 
  37. - $content (content of the element $tag) 
  38.  
  39. Addition 1999-01-22: 
  40. $html_file no longer needs to be a file but can also be a string. 
  41. */ 
  42.  
  43. function html_parse($html_file, $element_handler, $tag) 
  44. /* 
  45. This is quite fast, but it loads the entire file into memory. 
  46. */ 
  47. if (file_exists($html_file)) 
  48. $fd = fopen($html_file, "r") or die("Error: Unable to open file $html_file"); 
  49. $file_content = fread($fd, filesize($html_file)); 
  50. fclose( $fd ); 
  51. // Uncomment the following line if you've set magic_quotes_runtime! 
  52. //$file_content = stripslashes($file_content); 
  53. else 
  54. $file_content = $html_file; 
  55.  
  56. while ($full_tag = strstr($file_content, "<$tag")) 
  57. $full_tag = substr($full_tag, 0, strpos($full_tag, "</$tag>")+strlen($tag)+3); 
  58. $open_tag = substr($full_tag, 0, strpos($full_tag, ">")+1); 
  59. $open_tag = ereg_replace("(<$tag)|>", "", $open_tag); 
  60. // Split the string into key/value pairs: first split it into key=value, 
  61. // later into a hash. 
  62. $tmp_array = split ("[$"] +", $open_tag); 
  63. for ($i=0; $i<count($tmp_array); $i++) 
  64. $tmp_array[$i] = trim($tmp_array[$i]); 
  65. $tmp_array[$i] = ereg_replace(""", "", $tmp_array[$i]); 
  66. $tmp_attribs = split("=", $tmp_array[$i]); 
  67. for ($j=0; $j<count($tmp_attribs); $j++) 
  68. // Don't add empty pairs :) 
  69. if ($tmp_attribs[$j] != "" && $tmp_attribs[$j+1] != "") 
  70. $attribs[trim($tmp_attribs[$j])] = trim($tmp_attribs[$j+1]); 
  71. $content = substr ($full_tag, strpos($full_tag, ">")); 
  72. $content = substr($content, 1, strpos($content, "</$tag>")-1); 
  73. $element_handler($attribs, trim($content)); 
  74.  
  75. $file_content = substr($file_content, strpos($file_content, "</$tag>")+strlen($tag)+3); 
  76.