5

9 Advanced PHP String Functions You Must Master Today

 2 years ago
source link: https://www.makeuseof.com/php-advanced-string-functions-you-must-master-today/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

9 Advanced PHP String Functions You Must Master Today

By Unnati Bamania

Published 34 minutes ago

PHP has a huge number of useful string functions. If you're familiar with the basics, check out these more advanced functions for working with text.

PHP is an open-source server-side scripting language. You can embed PHP code into HTML to build web applications. Using PHP, you can easily develop dynamic web applications and connect such applications to a database.

Web-related tasks often involve text manipulation and PHP has many functions to help. PHP's string type offers a flexible and powerful means of working with text. In this article, you'll learn nine PHP string functions that you must learn today.

What are String Functions?

String functions are the built-in functions that help you to manipulate strings. You will typically call them by passing a PHP string variable for the function to operate on. Take a look at the example below. The strlen() function calculates the length of the string.

$var = "Hello World";
$len = strlen($var);
echo "The length of var is $len";

Output:

The length of var is 11

Related: How to Manipulate Text in PHP With These Functions

1. implode()

The implode() function converts an array of strings to a single string and returns it. This function accepts two parameters: the separator and the array of strings. Let's understand implode using an example:

$str = array("Hello", "Welcome", "to", "MUO");
echo implode(" ",$str);
echo implode("!",$str);
echo implode(".", $str);

Output:

Hello Welcome to MUO
Hello!Welcome!to!MUO
Hello.Welcome.to.MUO

2. ord()

The ord() function returns the ASCII value of the character passed to it.

echo ord('a');
echo ord('3');

Output:

97
51

Related: How Do You Find the ASCII Value Of a Character?

3. chr()

The chr() function is the opposite of ord. It returns the corresponding character when you pass its ASCII value. The ASCII value can be decimal, hexadecimal, or octal.

echo chr(52); // decimal
echo chr(0x23); // hexadecimal
echo chr(0176); // octal

Output:

4
#
~

4. md5()

The md5() function converts the given string to its MD5 hash. This function accepts two parameters: the string and a boolean parameter specifying the output format. By default, the output is a string containing 32 hexadecimal characters. If you want the output in binary form, pass true as the value of the second parameter.

$str = "Hello";
echo md5($str);

Output:

8b1a9953c4611296a827abf8c47804d7

5. chop()

The chop() function removes whitespace, or other characters, from the end of the given string. It accepts two parameters: the string variable and the characters you want to delete from it.

$str = "Hello! Welcome to MUO!!";
echo chop($str, "!");

Output:

Hello! Welcome to MUO

6. strcmp()

The strcmp() function compares two strings and returns a value accordingly. This function accepts two string arguments. If the two strings are equal, it will return 0. If the first string is less than the second, it will return a negative value. Otherwise, it returns a positive value.

echo strcmp("Hello MUO", "Hello MUO");
echo strcmp("Hello MUO", "hello muo");
echo strcmp("hello muo", "Hello MUO");

Output:

0
-32
32

7. strrev()

The strrev() function reverses the string.

$str = "Hello! Welcome to MUO";
echo strrev($str);

Output

OUM ot emocleW !olleH

8. ucfirst() & ucwords()

The ucfirst() function converts the first character of the string to uppercase. The ucwords() function converts the first character of all the words in a string variable to uppercase.

$str = "hello! welcome to MUO";
echo ucfirst($str);
echo ucwords($str);

Output:

Hello! welcome to MUO
Hello! Welcome To MUO

9. bin2hex() & hex2bin()

The bin2hex() function converts ASCII characters to their hexadecimal values. The hex2bin() function does the opposite.

$str = "Hello";
$str1 = "48656c6c6f";
echo bin2hex($str);
echo hex2bin($str1);

Output:

48656c6c6f
Hello

What’s Next After Learning the Basics of PHP?

You have learned to manipulate strings in PHP using some of its built-in functions. What's next? After learning the core concepts of any programming language, you should start practicing by building small projects. Building projects will improve your coding skills and help you get a good grasp of the language.

Looking for a good starting project using PHP? Authentication is a common task which you can use to learn more about PHP and MYSQL integration.

About The Author

613b99015a3e7-WhatsApp%20Image%202021-09-10%20at%2010.39.08%20PM.jpeg?fit=crop&w=100&h=100

Unnati Bamania (8 Articles Published)

Unnati is an enthusiastic full stack developer. She loves to build projects using various programming languages. In her free time, she loves to play the guitar and is a cooking enthusiast.

More From Unnati Bamania

Subscribe to our newsletter

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!

Click here to subscribe

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK