| |||||||||||
| |||||||||||
various preg style functions in use
String Handling in PHP example from a Well House Consultants training course
More on String Handling in PHP [link]
Source code: ex2.php Module: H107
<?php # As well as preg_match and preg_match_all, we have ...
$line = "404 SN1 6QL 17 4 80 2 3"; $stations = file("railstats.xyz"); print ("We have read ".count($stations). " records\n"); # Regular expression - the bits I DON'T want returned to me ;-) $gotten = preg_split("/\s+/",$line); # Typically the right thing. var_dump($gotten); # Much more descriptibe than .. print_r($gotten); # Dump variable content $gotten = preg_split("/\s*/",$line); # NOT what you mean. Splits every character. print_r($gotten); # Dump variable content # Preg_match is against a STRING and returns part of the string (if you give it a $gotten) # Preg_grep is against an ARRAY and returns complete array members and not parts of them. $bigchurch = preg_grep("/Abbey/",$stations); # Filter, returning only matching array members print_r($bigchurch); # Match and replace all in one go! $info = "Travel by train from Melksham to Westbury and on to Salisbury.\n"; $rv1 = preg_replace('/bury/','flower',$info); # All occurrences print ($rv1); $rv1 = preg_replace('/bury/','flower',$info,1); # Tell it how many print ($rv1); $rv1 = preg_replace('/bury/','flower',$info,-1,$tellme); # Tell me how many print ("$tellme changes - becomes $rv1"); $rv1 = preg_replace('/bury/','flower',$info); print ($rv1); $rv1 = preg_replace('/.bury/','.flower',$info); # input is RE, output is just a string print ($rv1); # so replaces "any char" with a . # If I want to use part of my input string in my output, capture and use \\1 etc $rv1 = preg_replace('/(.b)ury/','flower$1',$info); # Using part of matched input within output print ($rv1); # Match, compute using the matched string to generate the output function compass($got) { $rotate = array('North' => 'West', 'West' => 'South', 'South' => 'East', 'East' => 'North'); $result = $rotate[$got[1]]; return $result; } $rv1 = preg_replace_callback('/(North|South|East|West)/',"compass",$info); print ($rv1); /* ----------------------------------------- Sample output munchkin:re grahamellis$ php ex2.php We have read 2537 records array(8) { [0]=> string(3) "404" [1]=> string(3) "SN1" [2]=> string(3) "6QL" [3]=> string(2) "17" [4]=> string(1) "4" [5]=> string(2) "80" [6]=> string(1) "2" [7]=> string(1) "3" } Array ( [0] => 404 [1] => SN1 [2] => 6QL [3] => 17 [4] => 4 [5] => 80 [6] => 2 [7] => 3 ) Array ( [0] => [1] => 4 [2] => 0 [3] => 4 [4] => [5] => S [6] => N [7] => 1 [8] => [9] => 6 [10] => Q [11] => L [12] => [13] => 1 [14] => 7 [15] => [16] => 4 [17] => [18] => 8 [19] => 0 [20] => [21] => 2 [22] => [23] => 3 [24] => ) Array ( [8] => 1562 SAA AL1 2DN TL 144065 51.7447360734 -0.3425692916 St.Albans Abbey 174769 187112 207646 222482 217520 177470 [237] => 3235 FIT BS34 7JW ST 610786 51.504939615 -2.5624372568 Filton Abbey Wood 377552 401325 410630 457611 536958 598032 [626] => 6210 TNA DN39 6XF TA 108188 53.6539707522 -0.3234938147 Thornton Abbey 656 767 807 762 1102 1056 [1988] => 5131 ABW SE2 9RH TQ 472791 51.4907705898 0.1203255703 Abbey Wood 2201541 2089975 2804493 3096498 3029176 2882868 ) Travel by train from Melksham to Westflower and on to Salisflower. Travel by train from Melksham to Westflower and on to Salisbury. 2 changes - becomes Travel by train from Melksham to Westflower and on to Salisflower. Travel by train from Melksham to Westflower and on to Salisflower. Travel by train from Melksham to Wes.flower and on to Sali.flower. Travel by train from Melksham to Wesflowertb and on to Saliflowersb. Travel by train from Melksham to Southbury and on to Salisbury. munchkin:re grahamellis$ ---------------------------------------------------- */ ?> Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in PHP * PHP Programming * Beginning PHP - weekend course / hobby / club / leisure users * PHP Programming * Learning to program in PHP Also available on on site courses for larger groups 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 "String Handling in PHP" training module. You'll find a description of the topic and some
other closely related examples on the "String Handling in PHP" 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
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. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |