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

the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
Deploy PHP to Heroku
Feb 14, 2023 programming

Deploy PHP to Heroku

Today we are going to talk about deploying your PHP on heroku. Having your code on your local machine is one thing and deploying it on a server and sharing it with the world is another thing. ...

7 Min Read Read More
What to Do After Learning Programming
Mar 17, 2024 programming

What to Do After Learning Programming

Congratulations! You've learned programming and now have a solid foundation in coding. But what should you do next? ...

7 Min Read Read More
Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects
Jul 20, 2025 programming

Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects

Node.js has matured into one of the most essential tools in modern web development. Originally released in 2009, Node.js transformed JavaScript from a strictly frontend language into a robust backend solution capable of powering APIs, real-time applications, microservices, IoT systems, and more. ...

38 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 ...