EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP String Functions

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP String Functions

PHP String Functions

PHP String Functions

PHP inbuilt supports a few data types. Apart from these, PHP also supports many functions that are used while working on some data. PHP String functions are some of those functions that are used to manipulate string data. All these functions are predefined. There is a need for installing any plugins. Let’s look at some of PHP string functions.

Below are some of the string functions and examples are illustrated with the following syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

<?php
echo func( “<data>” );
?>

Examples of String Functions in PHP

String function is easy to use. Here we will discuss how to use string function in PHP programming with the help of examples

1. Addcslashes()

This returns a string with backslashes in front of specific characters

Eg:echo addcslashes ("Hello World!","W");

Output:

Hellow \World

2. Addslashes()

This returns string with backslashes in front of predefined characters

Eg:echo addcslashes(‘Hello “World” you’);

Output:

Hello \” World\” you

3. bin2hex()

Converts binary data into hexadecimal data

Eg:echo bin2hex (“Hello”);

Output:

48656c6c6f

4. chop()

Removes whitespaces or any predefined characters from the right end if specified

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Eg:echo chop (“WelcomeBack” , “Back”);

Output:

Welcome

5. chr()

This PHP string function returns the character of specified ASCII value

Eg:echo char(52);

Output:

4

6. chunk_split()

Used to split a string into smaller parts

Eg:echo chunk_split ($str, 2,", ");

Output:

We,lc,om,e,

7. convert_uudecode()

This decodes a uuencoded string

Eg:echo convert_uudecode ("+22!L;W9E( %!( 4\"$`\n` ");

Output:

I love PHP!

convert_uuencode() does the reverse of convert_uudecode()

8. count_chars()

This PHP string function outputs the data about the number of characters in a string

Eg:echo count_chars (“Hello”, 3);

Output:

Helo

Note: The integer value is the mode which is used to specify the type of output required

  • 0 – an array with the byte-value as key and the frequency of every byte as value.
  • 1 – same as 0 but only byte-values with a frequency greater than zero are listed.
  • 2 – same as 0 but only byte-values with a frequency equal to zero are listed.
  • 3 – a string containing all unique characters is returned.
  • 4 – a string containing all not used characters is returned.

9. crc32()

This calculates 32-bit cyclic redundancy checksum( A Mathematical function) of a string

Eg:crc32 ("Hello World!");

Output:

472456355

10. Implode()

This joins the array elements with a specified string

Eg:$array = array ('lastname', 'email', 'phone');
echo implode(",", $array);

Output:

lastname, email, phone

Note: join() also does the same. It is the alias of implode().

11. htmlspecialchars()

This converts some predefined characters to HTML entities i.e it shows the source

Eg:$str = “I am <b>Bold</b>”;
echo $str; => I am Bold
echo htmlspecialchars($str);

Output:

I am <b>Bold</b>

12. ltrim()

This PHP string function removes white spaces or predefined characters from the left of the string

Eg:echo ltrim (“Just a sample”, ”Just”);

Output:

a sample

Note: rtrim() does a similar job from right
trim() does the same from both ends.

13. number_format()

This formats the number with grouped thousands

Eg:echo number_format (1000000);

Output:

1,000,000

14. print()

This simply outputs the string and is slower than echo

Also, the print should not be used with ()

Eg:print “Hello”;

Output:

Hello

15. md5()

This calculates the md5 hash of the string

Eg:echo md5 (“Hello”);

Output:

8b1a9953c4611296a827abf8c47804d7

16. strtok()

This splits a string into smaller strings

Eg:$string = "This is to break a string";
$token = strtok ($string, " ");
echo($token); => This
To get all words of string,
while ($token !== false){
echo "$token<br>";
$token = strtok(" ");
}

Output:

This
is
to
break
string

17. strupper()

This converts a string to uppercase characters

Eg:echo strupper (“Beautiful Day”);

Output:

BEAUTIFUL DAY

Note: strlower() converts strings to all lowercase characters.

18. substr()

This returns part of the string starting with the index specified

Eg:echo subst (“A Hot Day”, 3);

Output:

ot Day

19. substr_replace()

This PHP string function replaces a part of the string with the string specified

Eg:echo substr_replace ("Hot", "Day",0);

Output:

Day

20. wordwrap()

This wraps a string to a number of characters

Eg:echo wordwrap (“Hello World”, 5, ”\n”);

Output:

Hello
World

21. Strlen()

This is used to determine the length of the string passed

Eg:echo strlen (“Hello”);

Output:

5

22. Strrev()

This PHP string function is used to get the reverse of the string

Eg:echo strrev (“welcome”);

Output:

emoclew

23. Strpos()

This returns the position of the first occurrence of a string inside a string

Eg:echo strops(“There you go”, “go”);

Output:

11

24. Str_repeat()

This repeats a string specified number of times

Eg:echo str_repeat (‘b’, 5);

Output:

bbbbb

25. Str_replace()

This PHP string function finds the specified word, replaces that with specified word and return the string

Eg:echo str_replace (“great”, “wonderful”, “have a great day”);

Output:

have a wonderful day

26. Nl2br()

This PHP string function inserts html line breaks in front of each new line of the string

Eg:echo nl2br (“Lets break \nthe sentence”);

Output:

Lets break <br />
the sentence

27. similar_text()

This calculates the similarity between two strings

Eg:echo similar_text ("Hello World","Great World");

Output:

7

28. sprintf()

This PHP string function writes a formatted string to a variable

Eg:echo sprintf (“There are %u wonders in the World”,7);

Output:

There are 7 wonders in the World

29. Str_ireplace()

This replaces characters in the string with specific characters. This function is case insensitive.

Eg:echo str_ireplace (“great”, “WOW”, “This is a great place”);

Output:

This is a wow place

30. str_shuffle()

This randomly shuffles all characters in a string

Eg:echo str_shuffle(“Hello World”);

Output:

lloeWlHdro

31. str_word_count()

This PHP string function returns the number of words in the given string

Eg:echo str_word_count (“a nice day”);

Output:

3

32. Strcspn()

This returns the number of characters before the specified character

echo strcspn ("Hello world!","w");

Output:

6

33. str_pad()

This function is used to pad to the right side of the string, a specified number of characters with specified character

Eg:echo str_pad (“Hello”, 10, ”.”);

Output:

Hello…..

34. Ord()

This PHP string function returns the ASCII value of the first character of the string

Eg:echo ord (“hello”);

Output:

104

35. Strchr()

Find the first occurrence of a specified string within a string

Eg:echo strchr ("Hello world!", "world");

Output:

world!

36. Strspn()

This returns the number of characters found in the string that contains characters from the specified string.

Eg:echo strspn ("Hello world!", ”Hl");

Output:

1

There are few more string functions available in PHP. The above string functions are commonly used functions in PHP for various requirements

Recommended Articles

This has been a guide to PHP String Function. Here we discussed how to use string function in PHP programming with the help of examples. You can also go through our other suggested articles to learn more–

  1. Introduction To PHP
  2. PHP Commands
  3. Career In PHP
  4. PHP Alternatives

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you