• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / PHP Strings

PHP Strings

Learn how to create and manipulate PHP strings, and how to use common PHP string functions.

Formatting Numbers with PHP’s number_format() Function

29 January 2010 / Leave a Comment

When displaying a number in a Web page, it’s nice to format the number in an easy-to-read way. For example, 123,456.78 is nicer to read than 123456.784321.

PHP’s number_format() function gives you an easy way to format numbers for displaying to the user. You can separate thousands with commas or other separators, choose the decimal point character, and choose the number of decimal places to display (or display no decimals at all).

[Read more…] about Formatting Numbers with PHP’s number_format() Function

Wrapping Text with PHP’s wordwrap() Function

19 January 2010 / 5 Comments

Occasionally your PHP script will need to break up a long line of text into several shorter lines while preserving whole words. This can happen if you’re formatting some text for printing or emailing, or you want to display preformatted text in a Web page using the pre element.

PHP gives you a handy function, wordwrap(), to do this job for you. This tutorial shows you how wordwrap() works.

[Read more…] about Wrapping Text with PHP’s wordwrap() Function

Padding PHP Strings with str_pad()

3 December 2009 / 1 Comment

In Trimming PHP Strings you learned how to remove unwanted whitespace characters from around a string. The opposite of trimming is padding — adding extra characters to either end (or both ends) of a string to make the string a certain length.

Padding is handy for lining up strings vertically for display, for adding leading zeroes to numbers, or for any situation where you need a string to be of a certain fixed length.

In this article you’ll explore PHP’s str_pad() function for padding strings.

[Read more…] about Padding PHP Strings with str_pad()

Trimming PHP Strings

26 November 2009 / 1 Comment

In programming-speak, trimming a string means removing unwanted characters — typically whitespace characters — from either end (or both ends) of the string. This is useful in a number of scenarios, such as:

  • Cleaning up user input, such as text field values in a submitted form
  • Removing unwanted line-break characters at the end of a line
  • Ensuring that a string is in the correct format for passing to another program

In this article you explore 3 useful PHP functions that you can use to trim strings:

  • trim() for trimming both ends of a string
  • ltrim() for trimming the beginning of a string, and
  • rtrim() for trimming the end of a string.

[Read more…] about Trimming PHP Strings

Formatting PHP Strings with printf and sprintf

19 November 2009 / Leave a Comment

Like many other languages, PHP features the versatile printf() and sprintf() functions that you can use to format strings in many different ways. These functions are handy when you need convert data between different formats — either to make it easy for people to read, or for passing to another program.

PHP features many other functions to format strings in specific ways — for example, the date() function is ideal for formatting date strings. However, printf() and sprintf() are great for general-purpose formatting.

In this tutorial you look at how to work with printf() and sprintf() to format strings.

[Read more…] about Formatting PHP Strings with printf and sprintf

Converting PHP Strings between Upper and Lowercase

29 October 2009 / Leave a Comment

One of the nice things about working with strings in PHP is its huge range of built-in string-handling functions. If you want to do something to a string, chances are there’s a PHP function to do it for you!

In this article you’ll look at PHP’s string functions for converting text between uppercase and lowercase:

  • strtolower() for converting a string to lowercase
  • strtoupper() for converting a string to uppercase
  • lcfirst() for converting the first letter to lowercase
  • ucfirst() for converting the first letter to uppercase
  • ucwords() for converting each word to uppercase

[Read more…] about Converting PHP Strings between Upper and Lowercase

Replacing Text in PHP Strings

19 October 2009 / 1 Comment

As well as searching strings in PHP, often it’s useful to replace text within strings too. For example, it can be handy for manipulating strings containing data, or when formatting text for displaying in a Web page.

PHP gives you a couple of useful functions for replacing text in a string:

  • str_replace() searches for one string of text and replaces it with another.
  • substr_replace() replaces text at a specified position within a string.

Read on to discover how to use these 2 useful PHP functions.

[Read more…] about Replacing Text in PHP Strings

Searching Strings in PHP

13 October 2009 / 7 Comments

When writing PHP scripts, you often need to search a string for a particular chunk of text. For example, you might be writing a search engine to search through pages of content, or you might want to know if a URL or email address contains a certain domain name.

PHP gives you many useful functions for searching strings. In this article you look at:

  • strstr() for finding out whether some text is in a string
  • strpos() and strrpos() for finding the position of some text in a string
  • substr_count() for finding out how many times some text appears in a string

[Read more…] about Searching Strings in PHP

Basic PHP String Operations

28 September 2009 / Leave a Comment

Now that you know how to create strings in PHP, it’s time to try out some basic string operations. In this tutorial you learn how to:

  • Join strings together
  • Find out the length of a string, and
  • Read and change individual characters within a string.

[Read more…] about Basic PHP String Operations

Creating Strings in PHP

14 September 2009 / 2 Comments

A string in PHP is any sequence of characters. Here are some examples of strings:

  • Hello, world!
  • abc123
  • #@@$%&*%

PHP lets you work with strings in many different ways. For example, you can search a string for a piece of text; extract characters from a string; and format a string to make it easier to read or use.

In this tutorial you learn how to create strings in your PHP scripts. You look at:

  • Creating strings with single quotes and double quotes
  • Avoiding confusion by using braces syntax
  • Escape sequences
  • How to create multi-line strings
  • Using your own delimiters to create strings

[Read more…] about Creating Strings in PHP

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits