Tips and Techniques example from a Well House Consultants training course
More on Tips and Techniques
[link]
This example is described in the following article(s): • Crossrefering documents with uniqueness and inconsistency issues - PHP proof of concept demo - [link] |
If you're searching for a page where you can try this code,
select here |
Source code: ppq.php Module: H312
<?php
/* Proof of concept demo ...
Match all patterns line (COPY 33/23/44) in a data stream, and produce a
unique list of all those used, with a list of the places they're used.
Patterns are "human edited" so may contain spurious spacing, leading zeros
and all that sort of thing.
*/
# Function to convert elements of a reference into a standard form
function canize($source,$k) {
# called as follows: $canonic = canize($gotten,$k);
# Reduce numbers to next integer down (also strip lead zeros)
$val1 = floor($source[2][$k]);
$val2 = floor($source[3][$k]);
$val3 = floor($source[4][$k]);
# Force key characters to upper case
$star = strtoupper($source[1][$k]);
# Rebuild Canonical reference
$canon = "($star $val1/$val2/$val3)";
return $canon;
}
# Set up the regular expression to be easy to follow!
# ---------------------------------------------------
# A 4 letter word in capitals, to be captured
$word4c = '\s*([A-Z]{4})\s*';
# White Space
$spaces = '\s+';
# A number - may have a decimal point and digits thereafter, to be captured
$floatc = '\s*(\d+(?:\.\d*)?)\s*';
# Set up an array of arrays to gather all references
$gather = array();
$im_desc = array();
# Find all the matches
# --------------------
foreach (file("robert") as $line) {
if (preg_match_all("!\( $word4c $spaces $floatc / $floatc / $floatc \)!x",
$line,$gotten)) {
# print $line; # Would print matched line
for ($k=0; $k<count($gotten[0]); $k++) {
# print ($gotten[0][$k]."\n"); # Identify each match
# Canonicalise each match (i.e. reduce to standard form removing human variance)
$canonic = canize($gotten,$k);
$parts = explode("\t",$line);
# Store image description too
$dt = explode("(",$parts[1]);
$im_desc[$parts[0]] = $dt[0];
# Is this a new reference? If so, create an array member to hold all its matches
if (! is_array($gather[$canonic])) {
$gather[$canonic] = array();
}
# Store crossreference (image name in this case) into appropriate array member
array_push($gather[$canonic],$parts[0]);
}
}
}
# We now have a table of all the matches. We can sort it and produce a
# result matrix for inclusion in a web page!
ksort($gather);
$html = "";
foreach (array_keys($gather) as $current_ref) {
$html .= "<tr valign=top><td>$current_ref</td><td>";
foreach ($gather[$current_ref] as $imgname) {
$html .= "<a href=http://www.wellho.net/pix/$imgname target=z>";
$html .= "$imgname";
$html .= "</a>";
$html .= " - \"$im_desc[$imgname]\"<br>";
}
$html .= "</td></tr>";
}
# ----------- In a real application the template (following) would
# ----------- be separated out to keep the look and feel apart from
# ----------- the business logic!
?>
<html>
<head>
<title>Finding all references within a data source</title>
</head>
<body>
<h1>Finding References</h1>
Scenario - we have a volume of data records that contains numerous
references of the form (XXXX DDD/DDD/DD) where XXXX are capital
letters and DD and DDD are digits. We want to find all those
references and list them, with the data key with which they are
associated. Some of the references may be duplicates, some
records may have multiple references, and as the refernces may
be human edited that are liable to be varied a bit - for example
we could find the number 16 represented as 16, 16.0, 16. and
even 0016.<br><br>
<b>Here are the RESULTS ... you can see the source code from links
at the base of this page. The data is
<a href=http://www.wellho.net/demo/robert>here</a></b><br>
<table><?= $html ?></table>
<br>
</body>
</html>
Learn about this subject
Books covering this topic
Yes. We have over 700 books in our library.
Books
covering PHP are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Tips and Techniques" training module. You'll find a description of the topic and some
other closely related examples on the
"Tips and Techniques" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from
our download centre under an
Open Training Notes License.
Other resources
• Our
Solutions centre provides a number of longer technical articles.
• Our
Opentalk forum archive provides a question and answer centre.
•
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the
resources centre.
• All of these resources can be searched through through our
search engine
• And there's a global index
here.
Web site author
This web site is written and maintained by
Well House Consultants.
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job.
Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are
forbidden from using it for running your own training courses
without our prior written permission. See
our
page on courseware provision for more details.
Any of our images within this code may NOT be reused on a public URL without our
prior permission. For Bona Fide personal use, we will often grant you permission provided
that you provide a link back. Commercial use on a website will incur a license fee for
each image used - details on request.