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
Function | What it Does |
---|---|
empty() | Tests if a string is empty |
strlen() | Calculates the number of characters in a string |
strrev() | Retrun reverse of a given string |
str_repeat() | Repeats a string no. of times you want |
substr() | Retrieves a section of a string |
strcmp() | Compares two strings |
str_word_count() | Calculates the number of words in a string |
str_replace() | Replaces parts of a string |
trim() | removes leading and trailing whitespaces from a string |
strtolower() | Converts in Lowercases a string |
strtoupper() | Converts in Uppercases a string |
ucfirst() | Converts in uppercase the first character of a string |
ucwords() | Converts in uppercases the first character of every word of a string |
addslashes() | Escapes special characters in a string with backslashes |
stripslashes() | Removes backslashes from a string |
htmlentities() | Encodes HTML within a string |
htmlspecialchars() | Encodes special HTML characters within a sting |
nl2br() | Replaces line breaks in a string with elements |
html_entity_decode() | Decodes HTML entities within a string |
htmlspecialchars_decode() | Decodes special HTML characters withing a string |
strip_tags() | Removes PHP and HTML code from a string |
Here’s example illustrating these operators in action
Eg i (use of empty( ) function)
<?php if(isset($_GET['sub'])) { if(empty($_GET['n'])) { echo "fill your name first"; } else { echo "welcome ".$_GET['n']; } } ?> <form> Enter your name<input type="text" name="n"/> <input type="submit" name="sub" value="show my name"/> </form>
Output : welcome Rexx
Enter your name
In the above example.
create a textbox and a submit button inside the form.
Pass value inside the textbox and click on button.then the value goes to PHP script page. $_GET is used to accept the value . empty() function is used to check the value which is entered by user.
If type something inside text box, it show a message welcome otherwise if value inside textbox in null (if statement execute) and show a message fill your name.
create a textbox and a submit button inside the form.
Pass value inside the textbox and click on button.then the value goes to PHP script page. $_GET is used to accept the value . empty() function is used to check the value which is entered by user.
If type something inside text box, it show a message welcome otherwise if value inside textbox in null (if statement execute) and show a message fill your name.
Eg ii (Use of strrev( ) function)
<?php $val="nitin"; if(strrev($val)==$val) echo "Your name is palindrome"; else echo "Your name is not palindrome"; ?>
Output : 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”
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 (Use of str_repeat( ) function)
<?php $val="welcome "; echo str_repeat($val,3); ?>
Output : 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.
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 (Use of str_replace( ) function)
<?php $str="welcome"; echo str_replace("e","@",$str); ?>
Output : w@lcom@
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”.
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 (Use of str_word_count( ) function)
<?php $str="hello user how r you"; echo str_word_count($str); ?>
Output : 5
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)
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 (Use of strcmp( ) function)
<?php $str="hello"; $str1="HELLO"; echo strcmp($str,$str1); ?>
Output : 1
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).
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(Use of strlen( ) function)
<?php if(isset($_GET['sub'])) { if(empty($_GET['n'])) { echo "<font color='red'>fill your name first</font>"; } else { if(strlen($_GET['n'])<5) { echo "<font color='red'>name must be greater than 5</font>"; } else { echo "welcome ".$_GET['n']; } } } ?> <form> Enter your name<input type="text" name="n"/> <input type="submit" name="sub" value="show my name"/> </form>
Output : name must be greater than 5
Enter your name
Eg viii (Use of strpos( ) function)
<?php $str="welcome"; echo strpos($str,"l"); ?>
Output : 2
Eg ix (Use of nl2br( ) function)
<?php $str1="hello user how r you"; echo nl2br($str1); ?>
Output : hello
user
how
r
you
In the above example
variable $str1 hold value=”hello
user
how
r
you”
nl2br() function replace newline character and output will become :”hello
user
how
r
you”
variable $str1 hold value=”hello
user
how
r
you”
nl2br() function replace newline character and output will become :”hello
user
how
r
you”
Eg x (Use of substr( ) function)
<?php $str="welcome to the world of php"; echo substr($str,24,3); ?>
Output : php
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 ).
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
now pass substr($str,24,3) function inside echo and the output will become: php
0 comments:
Post a Comment