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
PHP Date and Time
Mar 21, 2023 programming

PHP Date and Time

Today we are going to learn about date and time in PHP. knowing how to work with dates and times and how to work with different formats of dates is very important. ...

14 Min Read Read More
PHP Namespace
Mar 23, 2023 programming

PHP Namespace

Today we are going to talk about namespaces in PHP. Namespace is like putting the classes in a group. it helps you organize your code much better. It's a must-know topic for a PHP programmer. ...

7 Min Read Read More
PHP array change key case
Feb 14, 2023 programming

PHP array change key case

Today we are going to talk about changing the case of the keys of an array in PHP. Sometimes you might change the case of the keys in your array. It's very easy to do that in PHP. ...

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