Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
PHP preg functions - examples and comparision

preg_match('%CheckUpdateTime(.*?)Main content end%s',$stuff,$gotten);
preg_match_all('%(<td class="destination">.*?)</tr>%s',$gotten[1],$service);
foreach ($service[1] as $train) {
  $teapot = preg_split('%</td>%',$train);
  foreach ($teapot as $element) {
    $el = preg_replace('%\s+%',' ',trim(strip_tags($element)));
    print "$el\n";
    }
  print "-----------\n";
  }


For this week's course, I have been doing some quite complex data extraction in PHP ... and the example code above uses a number of the major PHP regular expression functions,

Firstly, PHP has an "ereg" family using POSIX regular expressions, and a "preg" family using Perl style. I have used the latter - a little harder to understand at first, but more powerful and stated to be quicker. You may as well take the bull by the horns if you're jumping in!

preg_match creates an array - the whole matched string in the first element, and parts that match bracketed sections of the regular expression in the second (number 1!) and subsequent elements.

preg_match_all works like preg_match but creates a whole array for each element. Slightly surprisingly, the first array within the array is all the full strings, the second array is all the fist matches, etc.

The return values from preg_match and preg_match_all are simply reports of success and not the matched parts, which are in the named array passed in as the third parameter.

preg_split throws away each occurrence of a matching string and returns, as an array, the unmatching string sections in between

preg_replace takes an incoming string and replaces all matches to a particular pattern with an alternative string.

See full source of the example above, which pulls back data from Transport for London and tells you about the next departures from your local tube station!
(written 2009-07-08, updated 2009-07-10)

 
Associated topics are indexed under
H106 - PHP - Arrays
  [3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
  [3379] Sorting data the way YOU want it sorted - (2011-08-05)
  [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
  [2215] If nothing, make it nothing. - (2009-06-02)
  [1614] When an array is not an array - (2008-04-17)
  [1451] More PHP sample and demonstration programs - (2007-12-01)
  [1199] Testing for one of a list of values. - (2007-05-22)
  [1116] PHP adding arrays / summing arrays - (2007-03-23)
  [832] Displaying data at 5 items per line on a web page - (2006-08-14)
  [773] Breaking bread - (2006-06-22)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [409] Functions and commands with dangerous names - (2005-08-11)

H312 - PHP - Tips and Techniques
  [2981] How to set up short and meaningfull alternative URLs - (2010-10-02)
  [2478] How did I do THAT? - (2009-10-26)
  [2025] Injection Attack if register_globals in on - PHP - (2009-02-04)
  [1983] Keeping PHP code in database and running it - (2009-01-09)
  [1958] PHP - Parse error: syntax error, unexpected $end ... - (2008-12-23)
  [1684] Two PHP include files to make easy web site indexing and navigation - (2008-06-21)
  [1667] Checking server performance for PHP generated pages - (2008-06-06)
  [1649] Easy conversion - image formats, currencies and distances - (2008-05-21)
  [1270] PHP Standalone - keyboard to screen - (2007-07-18)
  [1010] Dates, times, clickable diarys in PHP - (2006-12-28)
  [917] Syntax checking in PHP - (2006-11-07)
  [789] Hot answers in PHP - (2006-07-02)


Back to
Three recent questions on Tomcat Convertors
Previous and next
or
Horse's mouth home
Forward to
Debugging multipage (session based) PHP applications
Some other Articles
First courses for 2010
Python classes / courses - what version do we train on?
Who is Marc Schneider of Multilingual Search Engine Optimization Inc
Debugging multipage (session based) PHP applications
PHP preg functions - examples and comparision
Three recent questions on Tomcat Convertors
Monitoring and loading tools for testing Apache Tomcat
Dogs Trust, Dog Show, Newbury
Wiltshire - a chance to improve life for everybody
Sipping at York
3603 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, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73 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).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/2274_PHP ... ision.html • PAGE BUILT: Fri Feb 3 14:16:04 2012 • BUILD SYSTEM: wizard