PHP

PHP Uppercase And Lowercase Example

In this example we will show you how to manipulate strings from uppercase to lowercase and from lowercase to uppercase with PHP. This is neccesary when you need to format text.
For this example we will use:

  • A computer with PHP >= 5.5 installed
  • notepad++

 
 
 
 

1. Getting Started

Changing text from uppercase to lowercase or from lowercase to uppercase can become necessary when we are formating or reformatting text. We might need to do this, if for example we want the first letter of the first word in a paragraph to be in uppercase or we want all the words in a paragraph to be in lowercase.

According to library website PHP QR Code is open source (LGPL) library for generating QR Code, 2-dimensional barcode. Based on libqrencode C library, provides API for creating QR Code barcode images (PNG, JPEG thanks to GD2). Implemented purely in PHP.
some of the features of this library are

We want to convert the above text to:

According To Library Website PHP QR Code Is Open Source (LGPL) Library For Generating QR Code, 2-dimensional Barcode. Based On Libqrencode C Library, Provides API For Creating QR Code Barcode Images (PNG, JPEG thanks to GD2). Implemented Purely In PHP.
Some Of The Features Of This Library Are

In the text above we have successfully converted the first letter of each word to uppercase. PHP provides us with so many functions to carry out tasks like this. Lets take a look at some of this functions with some examples.

1.1 strtolower

strtolower converts a string to lowercase. The parameter or arguement supplied to this function is the string we intend to manipulate or convert.

String strtolower(String text);

The function above returns the lowercased string.

index.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	</style>

		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>

		<title>
Previewing The QR Code Image

</title>
		
		</head>
	<body>
	<?php
$text="According To Library Website PHP QR Code Is Open Source (LGPL) Library For Generating QR Code, 2-dimensional Barcode. Based On Libqrencode C Library, Provides API For Creating QR Code Barcode Images (PNG, JPEG thanks to GD2). Implemented Purely In PHP.
Some Of The Features Of This Library Are";
// returns  according to library website php qr code is open source (lgpl) library for generating qr code, 2-dimensional barcode. based on libqrencode c library, provides api for creating qr code barcode images (png, jpeg thanks to gd2). implemented purely in php. some of the features of this library are
echo strtolower($text);

?>
	</body>
	</html>

In the example above we converted the whole text to lowercase with the strtolower function.

1.2 strtoupper

strtoupper converts a string to uppercase. The parameter or arguement supplied to this function is the string we intend to manipulate or convert.

String strtoupper(String text);

The function above returns the uppercased string.

index2.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	</style>

		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>

		<title>
Previewing The QR Code Image

</title>
		
		</head>
	<body>
	<?php
$text="According To Library Website PHP QR Code Is Open Source (LGPL) Library For Generating QR Code, 2-dimensional Barcode. Based On Libqrencode C Library, Provides API For Creating QR Code Barcode Images (PNG, JPEG thanks to GD2). Implemented Purely In PHP.
Some Of The Features Of This Library Are";
// returns  ACCORDING TO LIBRARY WEBSITE PHP QR CODE IS OPEN SOURCE (LGPL) LIBRARY FOR GENERATING QR CODE, 2-DIMENSIONAL BARCODE. BASED ON LIBQRENCODE C LIBRARY, PROVIDES API FOR CREATING QR CODE BARCODE IMAGES (PNG, JPEG THANKS TO GD2). IMPLEMENTED PURELY IN PHP. SOME OF THE FEATURES OF THIS LIBRARY ARE
echo strtoupper($text);

?>
	</body>
	</html>

In the example above we converted the whole text to uppercase with strtoupper function

1.3 lcfirst

lcfirst converts the first character or letter of a string to lowercase. The parameter or arguement supplied to this function is the string we intend to manipulate or convert.

String lcfirst(String text);

The function above returns the first character of the string as lowercased.

index3.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	</style>

		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>

		<title>
Previewing The QR Code Image

</title>
		
		</head>
	<body>
	<?php
$text="According To Library Website PHP QR Code Is Open Source (LGPL) Library For Generating QR Code, 2-dimensional Barcode. Based On Libqrencode C Library, Provides API For Creating QR Code Barcode Images (PNG, JPEG thanks to GD2). Implemented Purely In PHP.
Some Of The Features Of This Library Are";
// returns  according To Library Website PHP QR Code Is Open Source (LGPL) Library For Generating QR Code, 2-dimensional Barcode. Based On Libqrencode C Library, Provides API For Creating QR Code Barcode Images (PNG, JPEG thanks to GD2). Implemented Purely In PHP. Some Of The Features Of This Library Are
echo lcfirst($text);

?>
	</body>
	</html>

1.4 ucfirst

ucfirst converts the first character or letter of a string to uppercase. The parameter or arguement supplied to this function is the string we intend to manipulate or convert.

String ucfirst(String text);

The function above returns the first character of the string as uppercased.

index4.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	</style>

		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>

		<title>
Previewing The QR Code Image

</title>
		
		</head>
	<body>
	<?php
$text="according to library website php qr code is open source (lgpl) library for generating qr code, 2-dimensional barcode. based on libqrencode c library, provides api for creating qr code barcode images (png, jpeg thanks to gd2). implemented purely in php. some of the features of this library are ";
//According to library website php qr code is open source (lgpl) library for generating qr code, 2-dimensional barcode. based on libqrencode c library, provides api for creating qr code barcode images (png, jpeg thanks to gd2). implemented purely in php. some of the features of this library are
echo ucfirst($text);

?>
	</body>
	</html>

1.5 ucwords

ucwords converts the first character of each word in a string to uppercase. The parameter or arguement supplied to this function is the string we intend to manipulate or convert.

String ucwords(String text);

The function above returns the first word of the string as uppercased.

index5.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	</style>

		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>

		<title>
Previewing The QR Code Image

</title>
		
		</head>
	<body>
	<?php
$text="according to library website php qr code is open source (lgpl) library for generating qr code, 2-dimensional barcode. based on libqrencode c library, provides api for creating qr code barcode images (png, jpeg thanks to gd2). implemented purely in php. some of the features of this library are ";
//According To Library Website Php Qr Code Is Open Source (lgpl) Library For Generating Qr Code, 2-dimensional Barcode. Based On Libqrencode C Library, Provides Api For Creating Qr Code Barcode Images (png, Jpeg Thanks To Gd2). Implemented Purely In Php. Some Of The Features Of This Library Are
echo ucwords($text);

?>
	</body>
	</html>

2. Summary

In this example we learnt how to convert strings from uppercase to lowercase and from lowercase to uppercase. We learnt about PHP ucwords, ucfirst, lcfirst, strtoupper, strtolower function.

3. Download the source code

Download
You can download the full source code of this example here: phpuppercaseandlowercase

Olayemi Odunayo

I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
pardeep
6 years ago

Nice useful for me but can develop with custom code instead of builtin function

Back to top button