This is a discussion on string functions and manipulations in php within the PHP Programming forums, part of the Web Development category; Hi, Strpos Vs Stripos Both function is used to findout the position of any charatcer in string content.The difference ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi, Strpos Vs Stripos Both function is used to findout the position of any charatcer in string content.The difference between these two function is Strpos is used to find position of first occurrence of a string and it is case sensitive while Stripos is case insensitive Note: stripos support only PHP5 and higher. PHP Code: 2 PHP Code: 3 PHP Code: 2 2 Thanks
__________________ K K Venkata Charya Success is easy to get but difficult to retain |
| Sponsored Links |
| |||
| Hi Strstr Vs Stristr The strstr() function searches for the first occurrence of a string inside another string.This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.This function is case-sensitive. The stristr() function searches for the first occurrence of a string inside another string.This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.This function is case-insensitive. PHP Code: PHP Code: Thanks
__________________ K K Venkata Charya Success is easy to get but difficult to retain |
| |||
| Hi chunk_split() function splits a string into a series of smaller part. But split() function split a string into array by regular expression. chunk_split() function does not alter the original string but split() function alter the string into array. The below is the details about the use of chunk_split() and split() functions chunk_split() chunk_split() function splits a string into a series of smaller parts. Syntax chunk_split(string,length,end) Parameter Description string Required. Specifies the string to split length Optional. A number that defines the length of the chunks. Default is 76 end Optional. A string that defines what to place at the end of each chunk. Default is \r\n Example In this example we will split the string after each character and add a "." after each split: <?php $str = "Hello world!"; echo chunk_split($str,1,"."); ?> The output of the code above will be: H.e.l.l.o. .w.o.r.l.d.!. Example In this example we will split the string after the sixth character and add "..." after each split: <?php $str = "Hello world!"; echo chunk_split($str,6,"..."); ?> The output of the code above will be: Hello ...world!... split() split() function Split string into array by regular expression split (string pattern, string string [, int limit]) Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the case-sensitive regular expression pattern. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the whole rest of string. If an error occurs, split() returns FALSE. Example: <?php // Delimiters may be slash, dot, or hyphen $date = "04/30/1973"; list($month, $day, $year) = split('[/.-]', $date); echo "Month: $month; Day: $day; Year: $year<br />\n"; ?> Last edited by Senthilkumar : 11-11-2007 at 10:42 PM. |
| |||
| str_shuffle() Definition and Usage str_shuffle() function is used to shuffles all the characters of a string randomly. One permutation of all possible is created. Syntax str_shuffle(string) Parameter Description string Required. Specifies the string to shuffle Example <?php echo str_shuffle("Hello World"); ?> The output of the code above could be: H leooWlrld |
| |||
| strchr() Definition and Usage strchr() function searches for the first occurrence of a string inside another string. This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found. This function is an alias of the strstr() function. Syntax strchr(string,search) Parameter Description string Required. Specifies the string to search search Required. Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number Note: This function is a binary safe and case-sensitive function. Example <?php echo strchr("Hello world!","world"); ?> The output of the code above will be: world! Example In this example we will search a string for the ASCII value of "o": <?php echo strchr("Hello world!",111); ?> The output of the code above will be: o world! strrchr() Definition and Usage strrchr() function finds the position of the last occurrence of a string within another string, and returns all characters from this position to the end of the string. If char cannot be found, this function returns FALSE. Syntax strrchr(string,char) Parameter Description string Required. Specifies the string to search char Required. Specifies the string to find. If this is a number, it will search for the character matching the ASCII value of that number Note: This function is binary safe. Example <?php echo strrchr("Hello world!","world"); ?> The output of the code above will be: world! Example In this example we will search a string for the ASCII value of "o": <?php echo strrchr("Hello world!",111); ?> The output of the code above will be: orld! |
| |||
| Hi vims, strcmp — Binary safe string comparison int strcmp ( string $str1, string $str2 ) Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. Note: This comparison is case sensitive. Code: $str1 = "Test"; $str2 = "Test"; $str3 = "Apple"; $str4 = "Zebra"; if (strcmp($str1,$str2) == 0) echo "Equal"; if (strcmp($str1,"Test") == 0) echo "Equal"; if (strcmp($str1,$str3) > 0) echo "$str1 > $str3"; if (strcmp($str1,$str4) < 0) echo "$str1 < $str4"; R.Kamalakannan. |
| |||
| Hi, strcasecmp - Binary safe case-insensitive string comparison int strcasecmp ( string $str1, string $str2 ) Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. Note: Binary safe case-insensitive string comparison. Code: <?php
$var1 = "Test";
$var2 = "test";
if (strcasecmp($var1, $var2) == 0) {
echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?> R.Kamalakannan. |
| |||
| Hi, I do not find much difference between these two but in my practical work i found these two have a difference in performance. rtrim is much faster than chop. Both rtrim and chop is used to remove a white space or other predefined character from the right end of a string where chop is an alias of the rtrim function. rtrim(string,charlist) chop(string,charlist) Example of chop function PHP Code: PHP Code:
__________________ K K Venkata Charya Success is easy to get but difficult to retain |
| |||
| substr_compare(): Binary safe comparison of 2 strings from an offset, up to length characters examples: echo substr_compare("abcde", "bc", 1, 2); //here 1 is the offset and 2 is length of comparison echo substr_compare("abcde", "de", -2, 2);
__________________ ....................... Thanks, Venkatbi... |
| |||
| Hi, Here i am discussing about uuencode and uudecode and its convert function. Uudecode Vs Uuencode A set of algorithms for converting files into a series of 7-bit ASCII characters that can be transmitted over the Internet. Originally, uuencode stood for Unix-to-Unix encode and uudecode for Unix-to-Unix decode but it has since become a universal protocol used to transfer files between different platforms such as Unix, Windows, and Macintosh. Uuencoding is especially popular for sending e-mail attachments. Nearly all e-mail applications support uuencoding for sending attachments and uudecoding for receiving attachments. It is used in string manipulation to convert the value as encode to decode or vice versa. PHP Code: Hello world! <?php $str = "Hello world!"; echo convert_uuencode($str); ?> Output ,2&5L;&\@=V]R;&0A ` Thanks
__________________ K K Venkata Charya Success is easy to get but difficult to retain |
| |||
| Hi htmlspecialchars() Vs htmlentities() The htmlspecialchars() function converts some predefined characters to HTML entities. The predefined characters are: & (ampersand) becomes & " (double quote) becomes " ' (single quote) becomes ' < (less than) becomes < > (greater than) becomes > Syntax string htmlspecialchars ( string string [, int quote_style [, string charset]] ) The htmlentities() function converts characters to HTML entities. Syntax string htmlentities ( string string [, int quote_style [, string charset]] ) Difference between htmlspecialchars and htmlentities htmlspecialchars only takes care of <, >, single quote, double quote and ampersand whereas htmlentities translates all occurrences of character sequences that have some other meaning in HTML. Also htmlentities( ) will check for non English language characters, such as French accents, the German umlaut, etc. PHP Code: Jane & 'Tarzan' PHP Code: My name is Øyvind Åsane. I'm Norwegian Thanks
__________________ K K Venkata Charya Success is easy to get but difficult to retain |
| |||
| strcspn strcspn — Returns the number of characters present in string before any part of mask is found. Syntax: int strcspn(string, mask); string string: String to search string mask: List of characters to find Description: strcspn() returns the number of characters that occur between the start of string and the first occurrence of any character listed in mask . strcspn() provides a simple way to parse strings and validate data. It is not commonly used in PHP - many other functions exist in the language that eclipse it in power and convenience. Example: Display names that don't start with a vowel <?php $list = array("Andrew", "Brigitte", "Chris", "Deb"); foreach($list as $name) { if(strcspn($name, 'aeiouyAEIOUY')) { echo $name, "\n"; } } ?> Output: Brigitte Chris Deb |
| |||
| strspn strspn — Finds the length of the initial substring containing only characters from mask . Syntax: int strspn(string, mask); string string: String to search string mask: Characters to find Description: strspn() returns the length of the substring at the start of string that contains only characters from mask . Given string abcdefand mask abc, for example, the function returns3. Example: Display names that start with vowels <?php $list = array("Andrew", "Brigitte", "Chris", "Deb"); foreach($list as $name) { if (strspn($name, 'aeiouyAEIOUY')) { echo $name, "\n"; } } ?> Output: Andrew |
| |||
| similar_text similar_text — Calculates the similarity of two strings. Syntax: int similar_text(string_one, string_two[, $percent]); string string_one: First string to be compared string string_two: Second string to be compared variable $percent (optional): Variable to store the percentage similarity of the strings Description: similar_text() calculates the similarity of two strings. The function returns the number of unique characters that appear in both strings. The function can store the percentage similarity of the strings in the optional $percent parameter. Example: A cheesy example <?php $term = 'cheese'; foreach(array('gouda', 'gruyere', 'cheddar') as $match) { echo similar_text($term, $match, $percent), " characters from '$term' were contained in '$match'.\n", "Overall, '$term' is a ", round($percent), "% match for '$match'\n\n"; } ?> Output: 0 characters from 'cheese' were contained in 'gouda'. Overall, 'cheese' is a 0% match for 'gouda' 2 characters from 'cheese' were contained in 'gruyere'. Overall, 'cheese' is a 31% match for 'gruyere' 3 characters from 'cheese' were contained in 'cheddar'. Overall, 'cheese' is a 46% match for 'cheddar' |
| |||
| sscanf sscanf — Parses input from a string according to a specified format. Syntax: mixed sscanf(string, format[, $variables]); string string: String to parse string format: Format to use mixed $variables (optional): One or more variables to store Description: sscanf() parses string into variables based on the format string. however, instead of formatting and transforming variables, it provides a template with which to parse a string into variables. This function accepts two or more arguments. If only two arguments are provided, the data parsed from string are returned as a numerically keyed array. If additional arguments are passed to the function, the data parsed out of string are stored in them. If there are more specifiers than variables to contain them, an error is generated. If the opposite is true, the extra variables contain NULL . Example: Parse data out of a simple formatted string <?php $string = 'age:27 height:1.83m weight:90kg'; sscanf($string, 'age:%d height:%fm weight:%dkg', $age, $height, $weight); // use var_dump to show the types, as well as the values var_dump($age, $height, $weight); ?> |