This slide is about Php string functions and how to use them.
Size: 335.61 KB
Language: en
Added: Mar 24, 2015
Slides: 36 pages
Slide Content
Created by Nikul Shah
STRING FUNCTIONS
NIKUL SHAH 1
STRLEN()
•<?php
•echo strlen("Hello Nikul!");
•?>
•/*output */
•12
NIKUL SHAH 2
STR_WORD_COUNT()
•<?php
•echo str_word_count("Some people tend to forget that kindness and manners are free.");
•?>
NIKUL SHAH 3
STRREV()
•<?php
•echo strrev("don't loss your hope.");
•?>
NIKUL SHAH 4
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");
•?>
•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.
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.
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
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