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

Courses