string functions
TRANSCRIPT
STR_WORD_COUNT()
• <?php
• echo str_word_count("Some people tend to forget that kindness and manners are free.");
• ?>
NIKUL SHAH 3
STRPOS()
• strpos() function searches for a specific text within a string.
• If a match is found, the function returns the character position of the first match. If no
match is found, it will return FALSE.
• <?php
• echo strpos("Destiny works.", "works");
• ?>
NIKUL SHAH 5
STR_REPLACE ------ STRTR
• $search = array('A', 'B', 'C', 'D', 'E');
• $replace = array('B', 'C', 'D', 'E', 'F');
• $subject = 'A';
• $trans = array('A' => 'B','B'=>'C','C'=>'D','D'=>'E','E'=>'F');
• echo str_replace($search, $replace, $subject);
• echo "<br/>";
• echo strtr($subject,$trans);
NIKUL SHAH 6
ADDSLASHES()
• <?php
• $str=addslashes("today is 'monday'");
• echo "$str";
• ?>
• //output
• today is \'monday\'
NIKUL SHAH 8
ADDCSLASHES()
• <?php
• $str=addcslashes("Nikul","k");
• echo "$str";
• ?>
• Be careful using addcslashes() on 0 (NULL), r (carriage return), n (newline), f (form feed),
t (tab) and v (vertical tab). In PHP, \0, \r, \n, \t, \f and \v are predefined escape sequences.
NIKUL SHAH 9
CHR()
• The chr() function returns a character from the specified ASCII value.
• The ASCII value can be specified in decimal, octal, or hex values. Octal values are
defined by a leading 0, while hex values are defined by a leading 0x.
• <?php
$str = chr(43);
$str2 = chr(61);
echo("2 $str 2 $str2 4");
?>
NIKUL SHAH 10
EXPLODE()
• The explode() function breaks a string into an array.
• Note: The "separator" parameter cannot be an empty string.
• <?php
• $str="Hello my name is 'Nikul'";
• print_r (explode(" ",$str));
• print_r(explode(',',$str,0));
• ?>
• Using limits parameter it will returns no of elements.
• This function can not run on echo.
NIKUL SHAH 11
IMPLODE()
• implode() function returns a string from the elements of an array
• <?php
• $str =array('Hello','Nikul','How','are','You?');
• print_r(implode(",",$str));
• ?>.
NIKUL SHAH 12
JOIN()
• Joins array into string. Same as implode()
• <?php
• $im=array("nikul","Shah");
• echo (join(" ",$im));
• ?>
NIKUL SHAH 13
MD5()
• This function Calculates the MD5 hash of a string.
• <?php
• $im=array("nikul","Shah");
• echo md5(join(" ",$im));
• ?>
• 4994812ba7d125a30e2b9c04e7b7c014
NIKUL SHAH 14
NL2BR()
• Inserts HTML line breaks in front of each newline in a string
• <?php
• $n="Nikul \nShah";
• echo nl2br($n);
• ?>
NIKUL SHAH 15
STR_SPLIT()
• This funciton splits string into an array.
• <?php
• $n="Nikul Shah";
• print_r(str_split($n));
?>
Array ( [0] => N [1] => i [2] => k [3] => u [4] => l [5] => [6] => S [7] => h [8] => a [9] => h )
NIKUL SHAH 16
STRCMP()
• The strcmp() function compares two strings.
• <?php
• echo strcmp("Nikul","Nik");
• ?>
• 2
NIKUL SHAH 17
STRTOLOWER()
• Converts string from uppercase to lower case.
• <?php
• echo strtolower("Nikul Shah");
• ?>
NIKUL SHAH 18
STRTOUPPER()
• Converts string from lowercase to upper case.
• <?php
• echo strtoupper("Nikul Shah");
• ?>
NIKUL SHAH 19
TRIM()
• This function removes the while space.
• <?php
• $str = "Nikul Shah!";
• echo $str . "<br>";
• echo trim($str,"Nik");
• ?>
• Nikul Shah!
ul Shah!
NIKUL SHAH 20
NUMBER_FORMAT()
• This function formats a number with grouped.
• <?php
• echo number_format("100000",1);
• ?>
• 100,000.0
NIKUL SHAH 21
RTIRM()
• Removes white space or other char. From right hand side of the string.
• <?php
• $str = "Nikul Shah!";
• echo $str . "<br>";
• echo rtrim($str,"l Sah!");
• ?>
• Nikul Shah!
Niku
NIKUL SHAH 22
STR_IREPLACE()
• Replaces some char of the string.
• <?php
• echo str_ireplace("Nikul", "hi","hey hello");
• ?>
• hey hello
NIKUL SHAH 23
STR_REPEAT()
• str_repeat() function repeats a string a specified number of times.
• <?php
• echo str_repeat("Nikul",10);
• ?>
• NikulNikulNikulNikulNikulNikulNikulNikulNikulNikul
NIKUL SHAH 24
STR_REPLACE()
• <?php
• $r=array("hello","hey", "Nikul","hello");
• print_r(str_replace("hello","hi",$r,$i));
• echo"<br>";
• echo "replacements are :$i";
• ?>
• Array ( [0] => hi [1] => hey [2] => Nikul [3] => hi )
replacements are :2
NIKUL SHAH 25
STR_WORD_COUNT()
• Counts the word in the string.
• <?php
• echo str_word_count("Nikul Shah");
• ?>
• 2
NIKUL SHAH 27
STRCASECMP()
<?php
• echo strcasecmp("Nikul","Nikul");
• ?>
• 0 If this function returns 0, the two strings are equal.
• 0 - if the two strings are equal
• <0 - if string1 is less than string2
• >0 - if string1 is greater than string2
NIKUL SHAH 28
STR_PAD()
• <?php
• $str = "Hello Nikul";
• echo str_pad($str,40,".");
• ?>
• Hello Nikul.............................
NIKUL SHAH 29
STRCSPN()
• Retruns the no of char found in a string before any part of some specified char are found.
• <?php
• $str = "Hello Nikul";
• echo strcspn($str,"N");
• ?>
• 6
NIKUL SHAH 30
STRCHR()
• Finds the first occurrence of a string inside another string . Alias strstr().
• This is case sensitive.
• <?php
• $str = "Hello Nikul";
• echo strchr($str,“Nikul");
• ?>
NIKUL SHAH 31
STRISTR()
• Finds the first occurrence of a string inside another string . Alias strstr().
• This is case insensitive.
• $str = "Hello Nikul";
• echo strchr($str,“nikul");
• ?>
NIKUL SHAH 32
STRIPOS()
• stripos() function finds the position of the first occurrence of a string inside another string.
• <?php
• echo stripos("today is Sunday","Sunday");
• ?>
NIKUL SHAH 33
STRSPN()
• Returns the number of characters found in a string that contains only characters from a
specified charlist
• <?php
• echo strspn("Nikul","ikul");
• ?>
• 0
NIKUL SHAH 34
CONVERT_UUENCODE()
• <?php
$str = "Hello Nikul!";
echo convert_uuencode($str);
?>
• convert_uudecode()
• $decodeString = convert_uudecode($encodeString);
echo $decodeString;
NIKUL SHAH 35