1 / 89

Chapter - 7

Chapter - 7. String & Patterns ( Built-in Function ). PHP: Hypertext Preprocessor. Outline. What is String ? Basic String Using String As Arrays Transforming String Comparing Strings Searching Strings Matching Against Mask String Replace Other String Function Arrays Functions.

doyle
Download Presentation

Chapter - 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter - 7 String & Patterns ( Built-in Function ) PHP: Hypertext Preprocessor

  2. Outline • What is String ? • Basic String • Using String As Arrays • Transforming String • Comparing Strings • Searching Strings • Matching Against Mask • String Replace • Other String Function • Arrays Functions

  3. Whatis String ? • A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support. See details of the string type. • A string literal can be specified in four different ways: • single quoted • double quoted • heredoc syntax • nowdoc syntax (since PHP 5.3.0)

  4. Basic Strings • String objects are a special type of container, specifically designed to operate with sequences of characters. • A string variable is used to store and manipulate text. • String variables are used for values that contain characters.

  5. Basic Strings • We using the string to display some notation . • The string, like a variable, will have to be created first. There are two ways to use a string in PHP - you can store it in a function or in a variable. In the example below, we will create a string twice - the first time storing it in a variable, and the second time - in a function, in our case - an echo.

  6. Basic Strings <?php $myString = "This is a string!"; echo "This is a string!"; echo $myString; ?>

  7.  Use some different functions & operators to manipulate the string.

  8. Using String As Arrays • You can access the individual characters of a strings as if they were members of an array. • $text = “abcdef”; Characters Index

  9. Using String As Arrays <?php $str = “abcdef”; echo $str[‘1’] ; // b //Or for($i =0 ;$i < srtlen($str); $i++) { echo $str[$i]; } ?>

  10. String Function • In PHP we have tow type of functions : • User-defined Function. • Built-in Function. • User-definedFunction : is the function created by user . • Built-in Function: is the function created by PHP , and ready to use. • The real power of PHP comes from its functions. • We just talk about Built-in Function in this chapter

  11. Transforming String • If given two arguments, the second should be an array in the form array('from' => 'to', ...). The return value is a string where all the occurrences of the array keys have been replaced by the corresponding values. The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again. string strtr ( string $str , array $replace_pairs )

  12. Transforming String <?php echo strtr("HillaWarld","ia","eo"); ?> Output : Hello World

  13. Transforming String • The strtr() If given three arguments, this function returns a copy of str where all occurrences of each (single-byte) character in from have been translated to the corresponding character in to, i.e., every occurrence of $from[$n] has been replaced with $to[$n], where $n is a valid offset in both arguments.; string strtr ( string $str , string $from , string $to )

  14. Transforming String <?php$arr = array("Hello" => "Hi", "world" => "earth");echo strtr("Hello world",$arr); ?> Output : Hi earth

  15. Comparing Strings • You can comparison between strings by more than one method ,the PHP when comparison it make convert the data type to other if the data type not the same.

  16. Comparing Strings <?php If(“123aa” == 123) { echo “done”; } ?>

  17. Comparing Strings • This comparison is case sensitive. • Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. int strcmp ( string $str1 , string $str2 )

  18. Comparing Strings <?phpecho strcmp("Hello world!","Hello world!"); ?> Output : 0

  19. Comparing Strings • Binary safe case-insensitive string comparison. • Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. int strcasecmp ( string $str1 , string $str2 )

  20. Comparing Strings <?php$var1 = "Hello";$var2 = "hello";if (strcasecmp($var1, $var2) == 0) {    echo '$var1 is equal to $var2 in a case-insensitive string comparison';} ?>

  21. Comparing Strings-example <?php $str = "Ali"; if(strcmp($str , "ali") == 0) { //we won't get here , becuse of case sensitivity } if(strcasecmp($str , "ali") == 0) { we well get here becuse strcasecmp() is case-insensitive } ?>

  22. Comparing Strings-example <?php $st1 = “abcd1234”; $st2 = “abcd5678”; echo strncasecmp ($st1,$st2,4); ?>

  23. Searching Strings • Find the numeric position of the first occurrence of needle in the haystack string. • Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1. • Returns FALSE if the needle was not found. int strpos ( string $haystack ,  $needle [, int $offset = 0 ] )

  24. Searching Strings <?phpecho strpos("Hello world!","wo"); ?> Output : 6

  25. Searching Strings-Example <?php $str =“abcdef”; $se_str = “abc”; If(strpos($str,$se_srt) !== false) { echo “found”; } ?>

  26. Searching Strings-Example <?php $str ='1234567'; $se_str = '123'; echo strpos($str,$se_str,1); //false ?>

  27. Searching Strings-Example <?php $str ='123456'; $se_str = ‘34'; echo strtr($str,$se_str,1); // 3456 ?>

  28. Matching Against Mask • Finds the length of the initial segment of subject that contains only characters from mask. • If start and length are omitted, then all of subject will be examined. If they are included, then the effect will be the same as callingstrspn(substr($subject, $start, $length), $mask) (see substr for more information). int strspn ( string $subject , string $mask [, int $start [, int $length ]] )

  29. Matching Against Mask <?phpecho strspn("Hello world!","kHlleo"); ?> Output : 5

  30. Matching Against Mask • Returns the length of the initial segment of str1 which does not contain any of the characters in str2. • Returns the length of the initial segment of subject which consists entirely of characters in mask. int strcspn ( string $str1 , string $str2 [, int $start [, int $length ]] )

  31. Matching Against Mask <?phpecho strcspn("Hello world!","w"); ?> Output : 6

  32. String Replace • This function returns a string or an array with all occurrences of search in subject replaced with the given replace value. • This function returns a string or an array with all occurrences of search in subject (ignoring case) replaced with the given replace value. • eplaces a copy of string delimited by the start and (optionally) length parameters with the string given inreplacement. str_replace ( $search ,  $replace ,  $subject [, int &$count ] ) str_ireplace (  $search ,  $replace ,  $subject [, int &$count ] ) substr_replace (  $string ,  $replacement ,  $start [,  $length ] )

  33. String Replace-example <?php echo str_raplace(“Mr”,”Welcom”,”Mr Ali”); ?> Welcom Ali

  34. String Replace -exmple <?php $a = 0; echo str_raplace(“a”,”b”,”a1a1a1”,$a); echo $a // 3 ?>

  35. String Replace -exmple <?php echo substr_replace("welecom ali ","hi",7); echo “<br>”; echo substr_replace("welecom my brother ali ","mr",9,10); ?> welecomhiwelecommmrali

  36. String Replace -exmple <?php $str = “123456789”; echo substr($str,0,3);//123 echo substr($str,1,1);//2 echo substr($str,-2);//89 echo substr($str,-2,1);//8 ?>

  37. String Functions • strtoupper()- Make a string uppercase • strtolower()- Make a string lowercase • ucfirst()- Make a string's first character uppercase • ucwords()- Uppercase the first character of each word in a string

  38. strtoupper() <?php$str = “make a string uppercase ";$str = strtoupper($str);echo $str; ?> MAKE A STRING UPPERCASE

  39. strtolower() <?php$str = “MAKE A STRING LOWERCASE ";$str =  strtolower($str);echo $str; ?> make a string lowercase

  40. ucfirst() <?php$str = “make a string's first character uppercase ";$str =  ucfirst($str);echo $str;  ?> Make a string's first character uppercase

  41. ucwords() <?php$str = “uppercase the first character of each word in a string ";$str =  ucwords($str);echo $str;  ?> Uppercase The First Character Of Each Word In A String

  42. str_replace() <?phpecho str_replace("world","Peter","Hello world!"); ?> Hello Peter!

  43. str_replace() <?php$arr = array("blue","red","green","yellow");print_r(str_replace("red","pink",$arr,$i));echo "Replacements: $i"; ?> Array ( [0] => blue [1] => pink [2] => green [3] => yellow ) Replacements: 1

  44. wordwrap() • Wraps a string to a given number of characters using a string break character. • Returns the given string wrapped at the specified length.

  45. wordwrap() <?php $text="The quick brown fox jumped over the lazy dog."; echo wordwrap($text,20,"<br/> \n"); ?>

  46. str_split () • Converts a string to an array. • If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length. • FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element. array str_split ( string $string [, int $split_length = 1 ] )

  47. str_split () <?php$str = "Hello Friend";$arr1 = str_split($str);$arr2 = str_split($str, 3);print_r($arr1);print_r($arr2); ?>

  48. str_word_count() • Specify the return value of this function. The current supported values are: • 0 - returns the number of words found • 1 - returns an array containing all the words found inside the string • 2 - returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )

  49. str_word_count() <?php$str = "Hello fri3nd, you're       looking          good today!";print_r(str_word_count($str, 1));print_r(str_word_count($str, 2));print_r(str_word_count($str, 1, 'àáãç3'));echo str_word_count($str);?>

  50. explode () • Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter. array explode ( string $delimiter , string $string [, int $limit ] )

More Related