Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
the array returned by preg_match_all
PHP supports two regular expression handlers - the "ereg" series that uses POSIX style regular expressions, and the "preg" series that uses Perl style. Why? Because both have their uses - the POSIX style makes for easier-to-read expressions and are said to be easier to learn, and the Perl style is more powerful and quicker in operation ... at the price of being harder to learn and follow.

I was updating some code in our Wiki yesterday and needed to search for all words in a string that start with a capital and include an embedded capital, since we use them as links - visit our shared data system to see what I mean. But then I hit a curious bug - introduced myself - where there were multiple hits. Turned out to be a good reminder that preg_match_all returns an ARRAY of ARRAYs - with each array contained being a list of each of the elements matching the revelant "interesting bits" in the regular expression given. Hmm - that might be clear as mud when I describe it, so here's an example:

<?php
$demo = "SwinDon ChippenHam MelkSham TrowBridge and WestBury";

preg_match_all('/([A-Z][a-z]+)([A-Z][a-z]+)/',$demo, $gotten);

foreach ($gotten as $set) {
foreach ($set as $item) {
print "$item ... ";
}
print ("<br>");
}
?>


You would EXPECT to get five arrays back - one for each of the place names. But actually you get just three - one for the complete names of the places, one for the start of the place name, and one for the end of the place name.


SwinDon ... ChippenHam ... MelkSham ... TrowBridge ... WestBury ...
Swin ... Chippen ... Melk ... Trow ... West ...
Don ... Ham ... Sham ... Bridge ... Bury ...


Once you've come across this for the first time, it's easy enough to handle in your program - but I just felt it was worth a Beware - counterintauative feature warning here. And there are flag options to the command that let you alter its behaviour if you wish
(written 2005-06-06 05:40:00)

 
Associated topics are indexed under
H107 - String Handling in PHP
H999 - Additional PHP Material

Back to
Targetted Advertising
Previous and next
or
Horse's mouth home
Forward to
OO techniques are hard to teach

Some other Articles
Happy Birthday, PHP
Code and code maintainance efficiency
Passing information into and out of PHP functions
OO techniques are hard to teach
the array returned by preg_match_all
Targetted Advertising
Sad priorities
Symbolic links and hard links
Do NOT follow links or read attachments in these emails
Looking up IP addresses
1638 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho