Playing with strings

Learning how to manipulate text strings is one of the more rewarding things to learn in PHP - there are many helpful functions in the language that you can make quick and easy use of that allow you to do all sorts of crazy things with user input.

In this section we will be starting off with a few key functions you are likely to be using regularly, but we will also be discussing some of the more niche functions that are just as useful. I have sorted these functions by their usefulness - if you only have a short time to read this book for some reason (you know, if you keep reading a page every time you visit your book store, you will need about three years!) you should learn the functions in order.

As with variables name PHP considers strings to be case sensitive, which means matching and replacing strings would be quite tricky if it were not for the fact that PHP provides case-insensitive versions of each function where it would be helpful. These are mentioned wherever they occur.

Once the basic matching functions are out of the way, we will be moving onto a tricky topic - regular expressions. Regular expressions, or regexes as they are more commonly known, are hard to learn, but provide an incredibly fast and flexible way to manipulate strings.

You will almost certainly be doing a lot of work with strings at some point, so it is strongly recommended that you read the whole of this section thoroughly, and try each of the examples yourself - remember, much programming, much power!

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Reading from part of a string >>

Previous chapter: Mathematical constants

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.