Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business hotel accommodation. You are welcome to browse and use our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
PHP module H107
String Handling in PHP
Exercises, examples and other material relating to training module H107. This topic is presented on public courses Learning to program in PHP, PHP Programming, Beginning PHP - weekend course / hobby / club / leisure users, PHP Programming, Learning to program in PHP

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.

Related technical and longer articles
Analysing incoming data lines
Pattern Matching - a primer on regular Expressions
Nasty Characters in Web Applications
An overview of PHP String functions
Web Page and HTML Spell Checker

Articles and tips on this subjectupdated
4072Splitting 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:   $first ...
4071Setting 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 ...
3788Getting 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 ...
3790Solution 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 ...
3789More 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 ...
3534Learning 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 ...
3515PHP - 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. ...
3516Regular 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 ...
3424Divide 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 ...
463Splitting 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 ...
31Here documents
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 ...
3020Handling (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 ...
2629Curly 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, ...
2046Finding 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 ...
2238Handling 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 ...
2165Making 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 /\(\s*([A-Z]{4})\s+(\d+(?:\.\d*)?)\s*\/\s*(\d+(?:\.\d*)?)\s*\/\s*(\d+(?:\.\d*)?)\s*\)/ We offer a Regular Expression Course that will help you understand ...
1799Regular 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 ...
558Converting 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 ...
422PHP 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 ...
1613Regular expression for 6 digits OR 25 digits
I can write a regular expression to match ANY number of digits between 6 and 25 ... ^[[:digit:]]{6,25}$ but how would I write a regular expression to match either 6 digits or 25 digits, but no number in between? ^([[:digit:]]{6}|[[:digit:]]{25})$ (The regular expressions in this example are Tcl and ...
1603Do 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 ...
1533Short 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 * ...
1372A 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 - ...
1336Ignore 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 ...
1195Regular 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). It's ...
1058PHP 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. eregi('href="([^"]+)"[^>]*>([^<]+)',$line,$parts); Far fetched example? No - it's part of a link checker that picks out a URL and the text that ...
1008Date 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. ...
589Robust 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 ...
560The 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 ...
574PHP - 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 strtok ...
54PHP and natural sorting
If you sort (8, 9, 10, 11, 12), how do you want them to be output? "In the order you've just listed, Graham", you'll probably say. But if they're strings and you sort them, then you'll get the order 10, 11, 12, 8, 9 output if you just use a default sort, because the character "1" comes before the character ...
716Evaluating 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, ...
728Looking 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 ...
Examples from our training material
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
String overview - Form to variable to SQL ... to variable to HTML
"There's a function to do that" - answer to all PHP questions?
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from [here].
Topics covered in this module
Strings - in variables and literals.
String functions - an introduction.
Low-level functions.
Higher level functions.
Matching, extraction and replacement functions.
String operations.
String cleansing.
Formatting data into a string - sprintf.
Functions for formatting web pages.
Low-level string manipulation - strpos and friends.
Splitting up a string.
Regular expressions.
What flavour of regular expressions?.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Ruby, Lua, Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses 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.

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/resources/H107.html • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb