Tuesday, 31 January 2017

STRING FUNCTIONS IN PHP

1. strlen() – To get length of string.

Example –
<?php
$str="Welcome";
echo strlen($str);
?>

Output –
7

Note – ord() is used for getting ASCII value.


2. strtoupper() – Converts all characters of a string into uppercase.

Example –
<?php
$str="Welcome";
echo strtoupper($str);
?>

Output –
WELCOME


3. strtolower() – Converts characters of a string into lowercase.

Example –
<?php
$str="WELCOME";
echo strtolower($str);
?>

Output –
welcome


4. ucfirst() – Converts first characters of a string into upper case.

Example –
<?php
$str="welcome";
echo ucfirst($str);
?>

Output –
Welcome


5. ucwords() – Converts first character of all words into uppercase.

Example –
<?php
$str="welcome to php";
echo ucwords($str);
?>

Output –
Welcome To Php


6. nl2br() – To break new lines of string.

Example –
<?php
$str="welcome
to php
Alien";
echo nl2br($str);
?>

Output –
welcome 
to php
Alien



7. str_word_count() – To get total number of words of a string. We can pass node value, if node value is 0 then it returns total number of words. If node value is 1 then it returns each word as array elements. Element has a numeric key 0,1,etc. If node value is 2 then it returns each word as array element, keys are the index numbers of words.

Example –
<?php
$str="welcome to php Alien";
print_r(str_word_count($str,2));
?>

Output –
Array
(
    [0] => welcome
    [8] => to
    [11] => php
    [15] => Alien
)

Example –
<?php
$str="welcome to php Alien";
print_r(str_word_count($str,1));
?>

Output –
Array
(
    [0] => welcome
    [1] => to
    [2] => php
    [3] => Alien
)

Example –
<?php
$str="welcome to php Alien";
print_r(str_word_count($str,0));
?>

Output –
4


8. strip_tags() – To strip HTML of a string.

Example –
<?php
$str="<input type='button' value='click'>Scott";
echo strip_tags($str);
?>

Output –
Scott


9. htmlspecialchars() – To stop execution of HTML tags.

Example –
<?php
$str="<b><i>Scott</i></b>";
echo htmlspecialchars($str);
?>

Output –
<b><i>Scott</i></b>


10. addslashes() – To add escape slashes (backslashes /) in front of single and double quotations.


11. stripslashes() – To remove the slashes, which we added with add slashes function.

Example –
<?php
$str="Wel'come";
$str1=addslashes($str);
echo $str1;
echo "\n";
echo stripslashes($str1);
?>

Output –
Wel\'come
Wel'come


12. str_replace() – To replace a string with the new string.

Example –
<?php
$str="Welcome to PHP Earth";
echo str_replace("Earth","World",$str);
?>

Output –
Welcome to PHP World


13. strchr() – To get substring of input string from specified characters.

Example –
<?php
$str="Welcome to PHP Earth";
echo strchr($str,"c");
?>

Output –
come to PHP Earth


14. strrchr() – Same as strchr(), but it searches the specified character from reverse direction of string.

Example –
<?php
$str="Welcome Scott";
echo strrchr($str,"c");
?>

Output –
cott


15. strstr() – It is same as strchr(), to get substring of a string from specified characters.


16. stristr() – Same as strstr(), but it is case insensitive.

Example –
<?php
$str="Welcome Scott";
echo stristr($str,"C");
?>

Output –
come Scott


17. similar_text() – To get similarities between two strings.

Example –
<?php
$str="Scott";
$str1="Scott";
$str2="Smtth";
echo similar_text($str,$str1);
echo "\n";
echo similar_text($str1,$str2);
?>

Output –
5
3

1 comment:

  1. Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. String functions php

    ReplyDelete