Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
An overview of PHP String functions

AN INTRODUCTION

PHP is a modern language, written by the open source community. Ever since it was written, users who are pushing it towards its limits have been encouraged to provide extra functions that stretch the limits further. This has resulted in a language with a huge array of functions, and it also means that what was an advanced, pioneering use of PHP a year or two ago has become a standard, run-of-the-mill use today. Modern computer power means that all these additional functions don't have any significant adverse effect on performance either, as they would have done with earlier generations of hardware.

The result of this is that if you say "I wish there was a function to do that", then there probably is. A lot of the effort involved in learning about PHP, once you get past the fundamental structures of the language, is in learning where to find the functions that you want.

String handling is one of the areas where there's a huge number of functions available; in order to help you learn about the various things you can do with string functions we have (somewhat artificially) divided them down into some more manageable groupings. In addition to these groupings:
  - There are operators such as . and .= which operate on strings
  - Strings can be handled as if they were arrays of characters

LOW LEVEL FUNCTIONS

Functions that you can use to find out how long a string is, where one string occurs for the first time in another, to replace part of one string (given a starting point and length) with another, etc. Also functions to compare strings to see which comes first / last in sorting.

Low level functions are (roughly) equivalent to the functions you have in a language such as C. You can do more or less anything with them, but you have a lot of code to write to do so. In PHP, the low level functions are important only where you have a specialist requirement that can't be met by the other types of functions.

HIGHER LEVEL FUNCTIONS

This group includes functions to encode and translate strings in various ways - ranging from forcing a whole string to upper or lower case and trimming off excess white space through to "webifying" - in other words, adding in HTML entities where there are special characters involved - and adding in / removing protection characters if you're going to save your data into a relational database.

We've also grouped functions that split a string into an array of parts (or tokenise the string in some way) into the higher level functions, and also functions such as strspn which lets you check that a string comprises only certain characters that are acceptable to you.

MATCHING, EXTRACTION AND REPLACEMENT FUNCTIONS

PHP supports a wide range of functions that lets you compare or match strings, ranging from checking whether two strings are the same through to much more powerful functions that let you look at a string and see if it looks like a pattern that you've defined. You can go further, extracting parts of your incoming string into an array based on which part of the pattern you've matched, or replacing pertinent parts of the incoming string with some other text.

Patterns that are used for matching in this way are known as "Regular Expressions


See also String handling in PHP - training module

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

String Handling in PHP
  [4072] Splitting the difference with PHP - (2013-04-27)
  [4071] Setting up strings in PHP - (2013-04-27)
  [3790] Solution looking for a problem? Lookahead and Lookbehind - (2012-06-30)
  [3789] More than just matching with a regular expression in PHP - (2012-06-30)
  [3788] Getting more than a yes / no answer from a regular expression pattern match - (2012-06-30)
  [3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
  [3516] Regular Expression modifiers in PHP - summary table - (2011-11-12)
  [3515] PHP - moving from ereg to preg for regular expressions - (2011-11-11)
  [3424] Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP - (2011-09-08)
  [3020] Handling (expanding) tabs in PHP - (2010-10-29)
  [2629] Curly braces within double quoted strings in PHP - (2010-02-09)
  [2238] Handling nasty characters - Perl, PHP, Python, Tcl, Lua - (2009-06-14)
  [2165] Making Regular Expressions easy to read and maintain - (2009-05-10)
  [2046] Finding variations on a surname - (2009-02-17)
  [1799] Regular Expressions in PHP - (2008-09-16)
  [1613] Regular expression for 6 digits OR 25 digits - (2008-04-16)
  [1603] Do not SHOUT and do not whisper - (2008-04-06)
  [1533] Short and sweet and sticky - PHP form input - (2008-02-06)
  [1372] A taster PHP expression ... - (2007-09-30)
  [1336] Ignore case in Regular Expression - (2007-09-08)
  [1195] Regular Express Primer - (2007-05-20)
  [1058] PHP Regular expression to extrtact link and text - (2007-01-31)
  [1008] Date conversion - PHP - (2006-12-26)
  [728] Looking ahead and behind in a Regular Expression - (2006-05-22)
  [716] Evaluating arithmetic expressions in configuration files - (2006-05-10)
  [642] How similar are two words - (2006-03-11)
  [608] Don't expose your regular expressions - (2006-02-15)
  [589] Robust PHP user inputs - (2006-02-03)
  [574] PHP - dividing a string up into pieces - (2006-01-23)
  [560] The fencepost problem - (2006-01-10)
  [558] Converting between acres and hectares - (2006-01-08)
  [493] Running a Perl script within a PHP page - (2005-11-12)
  [463] Splitting the difference - (2005-10-13)
  [422] PHP Magic Quotes - (2005-08-22)
  [337] the array returned by preg_match_all - (2005-06-06)
  [54] PHP and natural sorting - (2004-09-19)
  [31] Here documents - (2004-08-28)

resource index - PHP
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/solutions/php-an-o ... tions.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard