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:
$first ... | 2013-04-27 |
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 ... | 2013-04-27 |
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 ... | 2012-07-14 |
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 ... | 2012-06-30 |
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 ... | 2012-06-30 |
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 ... | 2011-12-03 |
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. ... | 2011-11-27 |
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 ... | 2011-11-27 |
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 ... | 2011-09-08 |
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 ... | 2011-03-01 |
31 | Here 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 ... | 2011-01-29 |
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 ... | 2010-10-29 |
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, ... | 2010-02-11 |
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 ... | 2009-08-31 |
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 ... | 2009-06-21 |
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
/\(\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 ... | 2009-05-10 |
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 ... | 2008-09-16 |
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 ... | 2008-06-11 |
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 ... | 2008-05-17 |
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 ...
^[[: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 ... | 2008-04-16 (short) |
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 ... | 2008-04-13 |
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
* ... | 2008-02-07 |
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 - ... | 2007-09-30 |
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 ... | 2007-09-07 |
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).
It's ... | 2007-05-21 |
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.
eregi('href="([^"]+)"[^>]*>([^<]+)',$line,$parts);
Far fetched example? No - it's part of a link checker that picks out a URL and the text that ... | 2007-01-31 (short) |
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. ... | 2006-12-28 |
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 ... | 2006-06-11 |
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 ... | 2006-06-09 |
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
strtok ... | 2006-06-09 |
54 | PHP 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 ... | 2006-06-05 |
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, ... | 2006-06-05 |
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 ... | 2006-05-22 |
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.
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?.
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.