note 33279 deleted from function.preg-match-all by sniper
| From: | sniper@php.net | Date: | Tue, 30 Sep 2003 12:23:31 +0000 |
| Subject: | note 33279 deleted from function.preg-match-all by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57440@lists.php.net to get a copy of this message | ||
Note Submitter: malone@N0SPAM.vwmark.com
----
I prefer this method of extracting HTML attributes, it returns more complete and concise
information. I'll use a #colour attribute example:
-----------------------------
// a string containing an entire .html page
$template_string = open_file($template_filename);
// basically, find six digit characters with digits 0-9 and/or letters from A to F that would
indicate a RGB value.
// the six digit characters also must precede the hex symbol #.
preg_match_all('/<([\w]+).*(\#(?:[A-F]|[0-9]){6}).*>/i', $template_string,
$template_colours);
print_r($template_colours);
-----------------------------
output:
Array
(
[0] => Array
(
[0] => <TABLE cellSpacing=0 cellPadding=0 width=100% bgColor=#ffffff border=0>
[1] => <TR bgColor=#000000>
[2] => <TR align=left bgColor=#93b793>
[3] => <table cellspacing=0 cellpadding=0 width=100% bgcolor=#ffffff border=0>
[4] => <td valign=top width=165 bgcolor=#99cc99 height=600>
[5] => <TABLE cellSpacing=0 cellPadding=0 width=100% bgColor=#ffffff border=0>
)
[1] => Array
(
[0] => TABLE
[1] => TR
[2] => TR
[3] => table
[4] => td
[5] => TABLE
)
[2] => Array
(
[0] => #ffffff
[1] => #000000
[2] => #93b793
[3] => #ffffff
[4] => #99cc99
[5] => #ffffff
)
}
... after scanning many HTML files and array_merging (combining) $template_colours from multiple
pages, that can be followed up with array_count_values() to show the most often used colour in a set
of pages. The data in [1] => Array describes which HTML tag uses what colour - you can also apply
array_count_values() to that.
You can change the '(\#(?:[A-F]|[0-9]){6})' (colour hex) expression in preg_match_all() if
you wish to extract different kinds of HTML attributes.
If that # expression unexpectedly pulls out attributes that are not colours, use the following more
specific preg_match_all() instead:
preg_match_all('/<([\w]+).*color=(\#(?:[A-F]|[0-9]){6}).*>/i',
preg_replace('/"/', '', $template_string), $template_colours);
Reason for this: I am writing a template converter program that automatically and rapidly extracts
contents from hundreds of old pages and then places them into new templates. This particular regular
expression is for gathering the colour theme of the "old" contents being transferred, then
textfields listing the most often used colours are displayed so users can edit the colour theme
before converting templates.