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 simple REST API
Feb 15, 2023 programming

PHP simple REST API

Today we are going to create a simple REST API in PHP. we are going to do it with pure PHP and with no frameworks. you can create a RESTful API in 70 lines of code. ...

25 Min Read Read More
PHP range
Feb 15, 2023 programming

PHP range

Today we are going to talk about range function in PHP. It's a very simple yet very powerful tool and can save you a lot of time. it's also more readable than the alternatives. ...

3 Min Read Read More
PHP Array Combine
Feb 14, 2023 programming

PHP Array Combine

Today we are going to talk about array combine in PHP. Array combine is another useful array function that creates an array by using one array for keys and another for its values. ...

3 Min Read Read More
Top Open Source Licenses: Understanding Your Options
May 07, 2024 programming

Top Open Source Licenses: Understanding Your Options

Open source licenses dictate how software can be used, modified, and distributed. Understanding the different types of licenses is essential for developer. ...

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