PHP String - php classes -

PHP String

PHP 6 Months 100% Job Assistance Program. for more info... call on 8588829328

PHP string functions

PHP has over 75 built-in String manipulation functions, supporting operations ranging from string repetition and reversal to comparison and search-and-replace.

Some of these important functions are

Sr Function What it Does
1 empty() Tests if a string is empty
2 strlen() Calculates the number of characters in a string
3 strrev() Retrun reverse of a given string
4 str_repeat() Repeats a string no. of times you want
5 substr() Retrieves a section of a string
6 strcmp() Compares two strings
7 str_word_count() Calculates the number of words in a string
8 str_replace() Replaces parts of a string
9 trim() removes leading and trailing whitespaces from a string
10 strtolower() Converts in Lowercases a string
11 strtoupper() Converts in Uppercases a string
12 ucfirst() Converts in uppercase the first character of a string
13 ucwords() Converts in uppercases the first character of every word of a string
14 addslashes() Escapes special characters in a string with backslashes
15 stripslashes() Removes backslashes from a string
16 htmlentities() Encodes HTML within a string
17 htmlspecialchars() Encodes special HTML characters within a sting
18 nl2br() Replaces line breaks in a string with
19 html_entity_decode() Decodes HTML entities within a string
20 htmlspecialchars_decode() Decodes special HTML characters withing a string
21 strip_tags() Removes PHP and HTML code from a string
22 md5() The MD5 message-digest algorithm is a widely used for cryptography
23 str_split() str_split() function splits(break) a string into an array.

Here’s example illustrating these operators in action


Eg ii ( strrev( ) )

Your name is palindrome

In the above example
Declare variable $val hold value=”nitin”. Here we use strrev() Function to give reverse of a string.
We pass strrev() function inside If condition. if the reverse string is equal to declared string.
it will print “Your name is palindrome” otherwise “Your name is not palindrome”

Eg iii (str_repeat( ) )

welcome welcome welcome

In the above example
Declare variable $val with value=”welcome”.
use str_repeat( ) function with two argument. first argument declare name of variable, second argument we define number of times print the value.
The output is (welcome welcome welcome) because we pass 3 second argument.

Eg iv ( str_replace( ) )


In the above example
Declare variable $str with value=”welcome”.
use str_replace( ) function. It accepts three argument: the search term, the replacement term, and the string on which perform replacement.
we have Passed str_replace(“e”,”@”,$str) and
the output is : W@lcom@ because “@” replaced by “e”.

Eg v ( str_word_count( ) )


In the above example
Use str_word_count( ) function is used to count the number of word in a string.
declare variable $str value=”hello user how are you”.
pass this function inside echo so the output is :5 (count words separated by space)

Eg vi ( strcmp( ) )


In the above example
declare two variable $str value=(“hello”)
$str1 with value=(“HELLO”)
Now compare two string using strcmp( ) function.
display the output i.e 1 because both variable doesn’t contain same value(one is in lowercase while other in uppercase).

Eg vii(strlen( ))

name must be greater than 5
Enter your name

Eg viii ( strpos( ) )


Eg ix ( nl2br( ) )


Eg x( substr( ) )


In the above example
substr( ) function is used to slice string into smaller section.
it accepts three argument: (given string ,the position at which start slicing , and the number of character return from the starting position ).

$str hold a string value=”welcome to the world of php”
now pass substr($str,24,3) function inside echo and the output will become: php

Latest Trending Technologies