More useful and more controllable alternative is

This preview shows page 11 - 15 out of 19 pages.

more useful and more controllable alternative is splitting a string into an array, where each element in the array is one portion of the string. The function str_split() or explode() will split a string into an indexed array. The function Syntax: $array = str_split(string[, length]);. <?php $str = "Hello Friend" ; $arr1 = str_split ( $str ); $arr2 = str_split ( $str , 3 ); print_r ( $arr1 ); //function print_r() prints human readable data from $arr1 print_r ( $arr2 );
?> //Output: Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => F [7] => r [8] => i [9] => e [10] => n [11] => d ) Array ( [0] => Hel [1] => lo [2] => Fri [3] => end ) The explode() function breaks a string into an array. Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter. The explode function is required and it specific the string delimiter, the sting is the string to be split and limit is an optional argument that specific the number array elements to return. Syntax: $array = explode(separator, string, limit) <?php $str = "Hello world. It's a beautiful day." print_r (explode( " " ,$str)); ?> //Output Array ( [0] => Hello [1] => world. [2] => It's [3] => a ;
[4] => beautiful [5] => day. ) Comparing Strings Operator The most important string operation in any programming language is the comparing functionality. The ability to compare two strings against each other and return a Boolean value of true if they match or the Boolean value of false if they do not match. PHP provides two possible ways of comparing strings. The first technique is using the ( == ) equal operating. if ( 'string1' == 'string1' ) { echo 'Strings match.' ; } else { echo 'Strings do not match.' ; } //Output: Strings match. // Notice the S is now capitalized in the second string if ( 'string1' == 'String1' ) { echo 'Strings match.' ; } else { echo 'Strings do not match.' ; } //Output: Strings do not match. String Comparison Functions The second way to compare strings is to use of the built in PHP functions. PHP provide a range of compare functions, depending on the relationship between the strings. The two most widely use basic compare functions are strcasecmp() and strcmp(). The both perform a string compare but strcmp() is case sensitive and strcasecmp() is not. <?php $var1 = "Hello" ; $var2 = "hello" ; if ( strcmp ( $var1 , $var2 ) !== 0 ) { echo '$var1 is not equal to $var2 in a case sensitive string comparison' ; } //Output: $var1 is not equal to $var2 in a case sensitive string comparison ?>
<?php $var1 = "Hello" ; $var2 = "hello" ; if ( strcasecmp ( $var1 , $var2 ) !== 0 ) { echo '$var1 is not equal to $var2 in a case sensitive string comparison' ; } else { echo '$var1 is equal to $var2 in a non-case sensitive string comparison' ; } //Output: $var1 is equal to $var2 in a non-case sensitive string comparison ?> Regular Expressions A regular expression is a method of representing a string matching pattern. Regular expressions enable strings that match a particular pattern within textual data records to be located and modified and they are often used within utility programs and programming languages that manipulate textual data. Regular expressions are extremely powerful. PHP developers commonly use regular expression to validate user input. Such input can include credit card numbers, phone numbers, zip code, etc.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture