Free cookie consent management tool by TermsFeed Generator PHP string functions | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP string functions

PHP string functions

Last Updated on Mar 21, 2023

What are we going to do?

Today we are going to send a text through different functions and learn what those functions do. So let’s get started

This is our original text

$text = 'this is a sample text';

String to Array

Let’s change it to an array

We can do it by using the function explode

It takes the separator as the first argument and the text as the second argument

$arrayOfText = explode(" ",$text);
// Array ( [0] => this [1] => is [2] => a [3] => sample [4] => text )

Array to String

Now let’s turn that array to a string

We can do it by using the function implode

It takes what the separator should be as the first argument the text as the second argument

$arrayToText = implode(" ",$arrayOfText);
// this is a sample text

String to Uppercase

Now let's make all the letters in our string to uppercase

$toUpper = strtoupper($arrayToText);
// THIS IS A SAMPLE TEXT

String to Lowercase

Let’s change them back to lower case

$toLower = strtolower($toUpper);
// this is a sample text

Uppercase First Letter

with ucfirst we can make the first letter of the string to uppercase

$ucFirst = ucfirst($toLower);
// This is a sample text

Uppercase First Letter of each word

with ucwords we can turn the first letter of every word in our string to uppercase. It’s very good for titles

$ucWords = ucwords($toLower);
// This Is A Sample Text

Clean up Extra Spaces

Let’s say we have a text with space around it and we want to remove all that extra space. We can do it easily by trim function

$withSpace = '         ' . $ucWords . '          ';

$trimmedText = trim($withSpace);
// This Is A Sample Text

New Lines to Html Breaks

Let’s say we have a text that has multiple lines. As you know in html \n doesn’t have any effects and instead <br> is used for adding the new lines. Php will take care of it and changes all the new files to a break

$withNewLine = $trimmedText . "\n this is the second line";
// This Is A Sample Text this is the second line
$newLineToBreak = nl2br($withNewLine);
// This Is A Sample Text
// this is the second line

Remove Html Tags

Sometimes we have a text that has a lot of html tags and we just want to get the text and remove all the tags. We can easily do that with strip_tags

$removeHtmlTags = strip_tags($withNewLine);
// This Is A Sample Text this is the second line

https://youtu.be/YSmieMzKrSo

Conclusion

Now you know about some of the useful string functions in PHP.

I recommend you to open a PHP files and try apply different string functions to a text.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • how to uppercase the whole text
  • how to lowercase the whole text
  • how to uppercase only the first letter
  • how to uppercase the first letter of each word
  • how to turn an string to array and vice versa
  • how to remove all the html tags
  • how to add html breaks instead of new lines
  • how to remove the space around the text

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Array Replace
Feb 14, 2023 programming

PHP Array Replace

Today we are going to talk about array replace function in PHP. Array replace is another array function that is very simple yet a very powerful function. ...

8 Min Read Read More
PHP Array Intersect
Feb 14, 2023 programming

PHP Array Intersect

Today we are going to talk about array intersect functions in PHP .If you want to find the items in your array that are present in the other arrays, array intersect is your friend. ...

5 Min Read Read More
Creating a Simple Jupyter Notebook in PHP
Aug 28, 2024 programming

Creating a Simple Jupyter Notebook in PHP

This tutorial will guide you through the steps to create a simple PHP-based notebook interface. The notebook allows you to write and run PHP code in a web browser, maintaining the state between code executions. ...

28 Min Read Read More
AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers
Jul 18, 2025 programming

AngularJS Beginner to Advance 2025: The Complete Guide for Modern Web Developers

In today’s web development landscape, AngularJS may no longer dominate headlines, but it still powers countless legacy applications. Many organizations—especially in finance, healthcare, and enterprise software—rely on AngularJS systems that require ongoing maintenance and enhancements. ...

43 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...