PHP String Functions. PHP built-in supports some data types. Special from these, PHP also holds various functions that are used while working on some data. PHP String functions are some of those functions that are used to manage string data. All these PHP functions are predefined. There is a requirement for installing any plugins. Let’s look at some of the PHP string functions.
Following are some of the string functions and parts are represented with the following syntax.
<?php echo func( "<data>" ); ?>
Examples of PHP String Functions
The string function is simple to use. Here we will consider how to use the string function in PHP programming with the help of examples.
Strlen()
It is used to decide the length of the string passed.
<?php echo strlen ("output"); ?>
Strpos()
It will returns the position of the first event of a string inside a string.
<?php echo strops("You may run", "run"); ?>
Str_repeat()
It repeats a string stated a number of times.
<?php echo str_repeat ('r', 10); ?>
Str_replace()
This is a PHP string function to detects the particularized word, return that with a particularized word, and return the string.
<?php echo str_replace ("Nice", "charming", "have a nice day"); ?>
similar_text()
It will calculates the similarity between two strings.
<?php echo similar_text ("Good World","Best World"); ?>
sprintf()
The PHP string function will write a formatted string to a variable.
<?php echo sprintf ("There are %u surprise in the World",8); ?>
str_shuffle()
It will randomly shuffles all characters in a string.
<?php echo str_shuffle("Output"); ?>
str_word_count()
It will returns the set of words in the given string.
<?php echo str_word_count "Today is nice day"); ?>
str_pad()
This function is used to pad to the right side of the string, a defined number of characters with the defined characters.
<?php echo str_pad ("Hello", 5, "."); ?>
Strchr()
Find the first appearance of a defined string within a string.
<?php echo strchr ("Good Soft!", "Soft"); ?>
wordwrap()
This covers a string to a number of characters.
<?php echo wordwrap ("Great World", 6, "\n"); ?>
substr_replace()
It will replaces a part of the string with the string defined.
<?php echo substr_replace ("Cool", "Day",1); ?>
substr()
The returns part of the string starting with the index defined.
<?php echo subst ("A Cool Day", 4); ?>
strupper()
The converts a string to uppercase characters.
<?php echo strupper ("Nice Day"); ?>
md5()
it will calculates the md5 hash of the string.
<?php echo md5 ("Soft"); ?>
chr()
It returns the character of specified ASCII value.
<?php echo char(22); ?>
Addcslashes()
This returns a string with backslashes in front of specific characters.
<?php echo addcslashes ("Great World!","W"); ?>