1 / 97

PHP

PHP. Strings. Outline. String Variables In PHP The Most Used Functions In PHP Arrays Functions Transforming String Comparing Strings Searching Strings String Replace. String Variables in PHP.

kemp
Download Presentation

PHP

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. PHP Strings

  2. Outline • String Variables In PHP • The Most Used Functions In PHP • Arrays Functions • Transforming String • Comparing Strings • Searching Strings • String Replace

  3. String Variables in PHP • 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.

  4. String Variables in PHP • String can be defined using one of several methods, we encapsulate them in single quotes or double quotes . • We using the string to display some notation .

  5. The Most Used Functions In PHP

  6. Strlen() • Returns the length of the given string. • The length of the string on success, and 0 if the string is empty. Int strlen(string)

  7. Strlen() <?php $str = "welcome in my profile"; echo strlen($str); ?>

  8. strtoupper() • Returns string with all alphabetic characters converted to uppercase. • Returns the uppercased string. string strtoupper ( string $string )

  9. strtoupper() <?php$str = “make a string uppercase ";$str = strtoupper($str);echo $str; ?>

  10. strtolower () • Returns string with all alphabetic characters converted to lowercase. • Returns the lowercased string. string  strtolower   ( string $string )

  11. strtolower() <?php$str = “MAKE A STRING LOWERCASE ";$str =  strtolower($str);echo $str; ?>

  12. ucfirst() • Returns a string with the first character of str capitalized, if that character is alphabetic. • Returns the resulting string. string ucfirst ( string $str )

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

  14. ucwords() • Returns a string with the first character of each word in str capitalized, if that character is alphabetic. • Returns the modified string. string ucwords ( string $str )

  15. ucwords() <?php$str = “uppercase the first character of each word in a string ";$str =  ucwords($str);echo $str;  ?>

  16. substr_count() • substr_count() returns the number of times the needle substring occurs in the haystack string. Please note that needle is case sensitive. • This function returns an integer. int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )

  17. substr_count() <?phpecho substr_count("Hello world. The world is nice","world");?>

  18. 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. • <?php// Example 1$chars  = “A B C D E F";$pieces = explode(" ",  $chars);echo $pieces[0]; // Aecho $pieces[1]; // B • ?> array explode ( string $delimiter , string $string [, int $limit ] )

  19. implode () • Join array elements with a glue string. • Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element. string implode ( string $glue , array $pieces ) string implode ( array $pieces )

  20. implode () <?php $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname,email,phone ?>

  21. 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 ] )

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

  23. str_repeat () • Returns input repeated multiplier times. <?phpecho str_repeat("-=", 10); ?> string str_repeat ( string $input , int $multiplier )

  24. strrev()  • Returns string, reversed. • Returns the reversed string. string strrev ( string $string )

  25. strrev()  <?phpecho strrev("Hello world!"); // outputs "!dlrow olle ?>

  26. 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 ]] )

  27. 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);?>

  28. str_shuffle () • str_shuffle() shuffles a string. One permutation of all possible is created. • Returns the shuffled string. string str_shuffle ( string $str )

  29. stringstr_shuffle(string$str) str_shuffle () <?php$str = 'abcdef';$shuffled = str_shuffle($str);// This will echo something like: bfdaececho $shuffled; ?>

  30. rand () • If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and getrandmax(). If you want a random number between 5 and 15 (inclusive), for example, use rand(5, 15) int rand ( void ) int rand ( int $min , int $max )

  31. rand () • <?phpecho rand() . "\n";echo rand() . "\n";echo rand(5, 15); • ?>

  32. round () • Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default). • The rounded value round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )

  33. round () <?phpecho round(3.4);         // 3echo round(3.5);         // 4echo round(3.6);         // 4echo round(3.6, 0);      // 4echo round(1.95583, 2);  // 1.96echo round(1241757, -3); // 1242000echo round(5.045, 2);    // 5.05echo round(5.055, 2);    // 5.06 ?>

  34. round () <?phpecho round(9.5, 0, PHP_ROUND_HALF_UP);   // 10echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10echo round(9.5, 0, PHP_ROUND_HALF_ODD);  // 9echo round(8.5, 0, PHP_ROUND_HALF_UP);   // 9echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8echo round(8.5, 0, PHP_ROUND_HALF_ODD);  // 9 ?>

  35. max() • If the first and only parameter is an array, max() returns the highest value in that array. If at least two parameters are provided, max()returns the biggest of these values.  max ( array $values )  max (  $value1 ,  $value2 [,  $value3... ] )

  36. max() • max() returns the numerically highest of the parameter values. If multiple values can be considered of the same size, the one that is listed first will be returned. • When max() is given multiple arrays, the longest array is returned. If all the arrays have the same length, max() will use lexicographic ordering to find the return value. • When given a string it will be cast as an integer when comparing.

  37. max() <?phpecho max(1, 3, 5, 6, 7);  // 7echo max(array(2, 4, 5)); // 5echo max(0, 'hello');     // 0echo max('hello', 0);     // helloecho max('42', 3); // '42'echo max(-1, 'hello');    // hello$val = max(array(2, 2, 2), array(1, 1, 1, 1)); // array(1, 1, 1, 1)$val = max(array(2, 4, 8), array(2, 5, 7)); // array(2, 5, 7)$val = max('string', array(2, 5, 7), 42);   // array(2, 5, 7)?>

  38. min() • If the first and only parameter is an array, min() returns the lowest value in that array. If at least two parameters are provided, min() returns the smallest of these values. • min() returns the numerically lowest of the parameter values.  min ( array $values )  min (  $value1 ,  $value2 [,  $... ] )

  39. min() • ?phpecho min(2, 3, 1, 6, 7);  // 1echo min(array(2, 4, 5)); // 2echo min(0, 'hello');     // 0echo min('hello', 0);     // helloecho min('hello', -1);    // -1?>

  40. str_pad () • This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit. • Returns the padded string. string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )

  41. str_pad () <?php$input = "Alien";echo str_pad($input, 10);                      // produces "Alien     "echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Alien"echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Alien___"echo str_pad($input, 6 , "___");               // produces "Alien_“ ?>

  42. wordwrap() • Wraps a string to a given number of characters using a string break character. • Returns the given string wrapped at the specified length. string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )

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

  44. Array Functions

  45. Using String As Arrays • You can acces the individual characters of a strings as if they were members of an array. Ex1 :$str = “abcdef”; echo $str[‘1’] ; // b Or for($i =0 ;$i < srtlen($str); $i++) { echo $str[$i]; }

  46. array_push() • array_push () Push one or more elements onto the end of array • Returns the new number of elements in the array. intarray_push ( array &$array , $var [, mixed $... ] )

  47. array_push() <?php$stack = array("orange", "banana");array_push($stack, "apple", "raspberry");print_r($stack); ?>

  48. array_pop() • array_pop — Pop the element off the end of array • Returns the last value of array. If array is empty (or is not an array), NULL will be returned. array_pop ( array &$array )

  49. array_pop() <?php$stack = array("orange", "banana", "apple", "raspberry");$fruit = array_pop($stack);print_r($stack); ?>

  50. array_merge() • array_merge — Merge one or more arrays • Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. • If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. • Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array. • Returns the resulting array. array array_merge ( array $array1 [, array $... ] )

More Related