Exercises, examples and other material relating to training module H107. This topic is presented on public courses
The text processing facilities in PHP are wide ranging. Functions are provided for all the common functionality (high-level and low-level) that's required by most users, and for much more besides. We have a saying in PHP Ð "There's a function to do that" Ð and nowhere does it apply more than in text processing.
|Articles and tips on this subject||updated|
|4072||Splitting the difference with PHP|
There are lots of ways of splitting a string into pieces - you can do it character by characters, at a specific character, and word by word. PHP provides functions for splitting strings in these ways - and more - into arrays of shorter strings.
To split a string into a list of single character strings:
|4071||Setting up strings in PHP|
Inside, the web is all about passing around and manipulating strings of text - so the programming langauges of the web such as PHP have to be really strong in their string handling facilities. I give you two new examples from last week's course ... easy lessons that form the bedrock on which the powerful ...
|3788||Getting more than a yes / no answer from a regular expression pattern match|
Guy walks up to me in the street and asks "Could you direct me to the Town Centre?". So I answer him "yes, I could", and walk on. Does he thank me? No - he probably thinks "what a useless half answer" or "how rude can you be", even though I completely and correctly answered his question.
It's sometimes ...
|3790||Solution looking for a problem? Lookahead and Lookbehind|
There are some things that it's just plain difficult to find good, practical examples to show delegates on training course. "Solutions looking for problems" I call them sometimes. And one such group are lookahead and lookbehind in regular expression.
However, I think I did a bit better yesterday ...
|3789||More than just matching with a regular expression in PHP|
There's much more that you can do with regular expressions than just see if a string matches. Here are some examples in PHP, as I was teaching a course in PHP yesterday.
1. You can match, you can match and capture some or all of the matched string, and you can multiple match (and capture) ... see ...
|3534||Learning to program in PHP - Regular Expression and Associative Array examples|
There's a lot to learn on a week-long PHP course - especially if you're new to programming. And delegates need practice during and after the course with some relatively straightforward (but rewarding) real applications. One of the things that we do ourselves is to analyse server log files - allowing ...
|3515||PHP - moving from ereg to preg for regular expressions|
If your PHP scripts use "ereg" functions for regular expression matching, you should be aware that as from PHP version 5.3 they have been deprecated, and in PHP 6.0 the plan is to remove them completely.
The regular expressions move from the POSIX standard to the Perl standard. The function names change. ...
|3516||Regular Expression modifiers in PHP - summary table|
PHP's ereg functions are deprecated, and you should now be moving to the preg functions. See [here]. As you're switching your PHP code from ereg to preg regular expressions, you'll be adding delimiters to the main regular expression and providing the ability to add modifiers onto the end - a series ...
|3424||Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP|
If you divide £10,000 between 17 lucky children, each gets £588.23 ... but if you divide the number 10,000 by the number 17, you get 588.235294117647. How do you format the data to the appropriate number of decimal places?
In Ruby, you can use the % operator on a string - which is an alias ...
|463||Splitting the difference|
Perl's split function takes a string of text, and divides it up at a delimiter of your choice into a list of shorter strings ... it's one of the "power tool" functions of Perl and a vital part of the language. So how come that you can write a Tcl program and use its version of split - or omit the split ...
If you find yourself repeating something within a piece of code .... there's probably an easier way. After all, most programmer are lazy and that applies just as much (if not more) to the authors of the languages. Remember to use functions / methods / subs / procs / defs rather than cut and paste, and ...
|3020||Handling (expanding) tabs in PHP|
Question - Is a tab 8 spaces or 4?
Answer - Not quiet either, really. It's a movement of the cursor forward by up to 8 spaces (or 4 spaces or some other number if you have altered your tab stops).
We have many sample programs and data files on our web site (there's a full list, together with all ...
|2629||Curly braces within double quoted strings in PHP|
In PHP, you can include a variable name within a double quoted string and the variable will be expanded as the string is used - this means that double quotes are actually an operator, whereas single quotes mean a string literal. You can also put curly braces around a variable name to say, explicitly, ...
|2046||Finding variations on a surname|
Here's a little demonstration of how you can use PHP to look for a 'similar word' - in this case, from a file of surnames. Have I done all the hard work? No - I crawled for some surnames to add to my demonstration file, and I used PHP's built in functions to do the hard work!
Run Demonstration ...
|2238||Handling nasty characters - Perl, PHP, Python, Tcl, Lua|
Are your writing or maintaining a web based application that uses forms? If so, you have better be aware of some of the nasty characters that are around!
The < character, when echoed back from a users's input 'unchallenged', may form the start of a tag. So that in a relatively benign case, a user ...
|2165||Making Regular Expressions easy to read and maintain|
Have you ever seen a long regular expression made up of so many special characters that you can't read or maintain it very easily? Something like
We offer a Regular Expression Course that will help you understand ...
|1799||Regular Expressions in PHP|
Regular Expressions allow you to check if a string of text matches a particular pattern - for example to see if the data that a user has entered into a form looks like a Postcode / zipcode ... and then to extract the vital parts of the string that you're checking into separate variables.
Down the left ...
|558||Converting between acres and hectares|
I wanted to do a few conversions between hectares and acres and - guess what - I didn't know the conversion values off the top of my head. Then I may have a few other area conversion to do too, and I could do with some more PHP examples. Guess what ... a program ... and one worth sharing too
If you ...
|422||PHP Magic Quotes|
Do you want to read a string from a form and save it into a database? "Easy" you might say ... and indeed it is ... but if you just take the exact characters that were entered into the form and embed them in your SQL, you're laying yourself open to an injection attack. That's where a user enters a ...
|1613||Regular expression for 6 digits OR 25 digits|
I can write a regular expression to match ANY number of digits between 6 and 25 ...
but how would I write a regular expression to match either 6 digits or 25 digits, but no number in between?
(The regular expressions in this example are Tcl and ...
|1603||Do not SHOUT and do not whisper|
"Your Name" says a box on a form on our web page, and visitors who want to sign up duely enter their name. Some of the names are shouted - "GRAHAM ELLIS" - and others are whispered - "graham ellis" when really we would like them all nice and consistent and in regular case - for we're publishing a list ...
|1533||Short and sweet and sticky - PHP form input|
Today I have a short and (very) sweet PHP demo that calls for a user to enter a few pieces of information, and validates them - prior to storage in a database, perhaps.
Why is it "sweet"?
* Because it implements techniques such as sticky fields so that user who make error don't have to keep re-entering
|1372||A taster PHP expression ...|
I've been working on RSS feeds ... looking to get a page that combines various blogs so that I can have a quick look and see where we stand without having to hop and skip round each in turn. And with magpierss, it's turned out to be quite easy.
Yet some blogs give a summary in the their RSS feeds - ...
|1336||Ignore case in Regular Expression|
Do you want to ignore case in a regular expression? There are a variety of ways of doing it ... depending on the language you're writing. Here are some hints:
/abcd/i Perl - an i after the regular expression
eregi PHP - use eregi rather than ereg
re.I or re.IGNORECASE Python - extra parameters ...
|1195||Regular Express Primer|
Over the years I've been teaching people about Regular Expressions, I've learnt what does and doesn't work in such tuition. A casual question I saw yesterday got me writing, and I've just posted up a new technical article to the solution centre - see here (it will open in a separate window).
|1058||PHP Regular expression to extrtact link and text|
How many special characters can you realistically use in a regular expression without a single space, letter or digit? Here's one I wrote yesterday.
Far fetched example? No - it's part of a link checker that picks out a URL and the text that ...
|1008||Date conversion - PHP|
A simple piece of code to show you some examples of date reformatting in PHP. Frequently, you'll get dates in a format such as the ones I've started with here and want to re-arrange them, or do something more sophisticated.
My first re-arrangement simply explodes the date string and reforms it. ...
|589||Robust PHP user inputs|
If you write a script in PHP, it's one thing handing benign user inputs, and quite another "bullet proofing" your script against awkward characters (of the "less than" and "double quote" type) entered into form fields where they can lead to problems of the SQL error, echo display corruption and injection ...
|560||The fencepost problem|
If you erect a fence of 10 panels, you'll need 11 fenceposts to hold it up. And if you write a program that joins together 10 elements on a line, you'll only need 9 separators between them - this is known as the "fencepost problem".
If you write a simple program loop to output each element from a list ...
|574||PHP - dividing a string up into pieces|
"There's a function to do that ..." is the oft-used answer to many questions asked of PHP, but naturally that's followed by a plea of "what's it called". If you're looking to divide a string into a series or pieces, choose from these.
explode Returns an array, elements divided at a particular separator
|642||How similar are two words|
Do you want to help your web site user find what he's looking for on your web site, even if he mis-spells a name or word in a search? PHP provides you with three facilities - soundex, metaphones and Levenshtein distance calculations - which let you compare two words and see how similar they are when ...
|716||Evaluating arithmetic expressions in configuration files|
An interesting question came up in a consultants session this evening - how to take an expression that's held in a configuration file such as (A3+C3)*(A5+E6) and have the program work out the expression in the "conventional" programming sense - i.e. BODMAS (Brackets, O???? Division, Multiplication, Addition, ...
|728||Looking ahead and behind in a Regular Expression|
Regular expressions in Perl and PHP include facilities called zero width assertions, zero width lookahead and lookbehinds. A case of jargon that looks almost calculated to confuse?
Zero width assertions are where a regular expression matches some sort of condition in the line, without actually consuming ...
|20101101_statlog.txt|| Sample data from server operation log file|
|arrivals.php|| Regex and associative arrays - search engine arrivals|
|bits.php|| Ways of writing strings|
|bustimes.php|| Using PHP to format data (timetable in this example)|
|clformat.php|| Formatting data [file] for quick web display|
|code|| Reaping from an HTML page with regular expressions|
|dlog.php|| String handling example - extracting simple data from a complex line|
|echovalley.php|| How to put in blocks of HTML|
|ex2.php|| various preg style functions in use|
|extrawords|| Extra words for spell checker|
|fvar.php|| Filtering of variables|
|myform.php|| Robust, sticky form demo with field validation|
|mystdinc.inc|| Include file for sticky fields demo|
|pc1|| ereg v preg|
|pmoffs.php|| Capturing regular expression offsets|
|popage.php|| Counting in a log file - associative arrays and regular expressions|
|regex.php|| Regular expression - validating an email address|
|s1.php|| Single and double quoted strings|
|s2.php|| Character string as an array|
|s3.php|| String cleansing with functions such as strspn|
|secdo.php|| Convert seconds to minutes and seconds|
|sfex.php|| Demo of sticky fields and security|
|shunt.php|| Evaluting BODMAS expression using MySQL cell tables|
|spell.php|| Spell Checker|
|splitting.php|| Ways of splitting a string|
|sprint.php|| Formatting with sprintf|
|stdist.php|| Search - do two words look or sound the same?|
|stickform.php|| Stick Fields and Input Validation|
|strp.php|| Curly braces in double quoted strings|
|toke.php|| String tokenising - strtok, implode, explode|
Strings - in variables and literals.
String functions - an introduction.
Higher level functions.
Matching, extraction and replacement functions.
Formatting data into a string - sprintf.
Functions for formatting web pages.
Low-level string manipulation - strpos and friends.
Splitting up a string.
What flavour of regular expressions?.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.