200 likes | 294 Views
PHP: Hypertext Preprocessor. About PHP. PHP is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. String In PHP.
E N D
About PHP • PHP is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML
String In PHP • A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. • It is no problem for a string to become very large. There is no practical bound to the size of strings imposed by PHP,
A string literal can be specified in three different ways. • Single quoted • double quoted • Heredoc
Single quoted • The easiest way to specify a simple string is to enclose it in single quotes (the character '). • Eg • To specify a literal single quote, you will need to escape it with a backslash (\) • Eg
Double quoted • If the string is enclosed in double-quotes ("), PHP understands more escape sequences for special characters like (\n,\t etc): • But the most important feature of double-quoted strings is the fact that variable names will be expanded. • eg
Heredoc Syntax • Another way to delimit strings is by using heredoc syntax ("<<<"). One should provide an identifier after <<<, then the string, and then the same identifier to close the quotation. • Heredoc support was added in PHP 4. • eg
String Operations • There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side. • eg
String Functions • These functions all manipulate strings in various ways. • Using the strlen() function • The strlen() function is used to find the length of a string. • Eg
Using the strpos() function • The strpos() function is used to search for a string or character within a string. • If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE. • eg
the strcmp() function compares two strings. • This function returns: • 0 - if the two strings are equal • <0 - if string1 is less than string2 • >0 - if string1 is greater than string2 • Syntax • strcmp(string1,string2) • Eg:
strncmp() function • This function is similar to strcmp(), with the difference that you can specify the (upper limit of the) number of characters from each string to be used in the comparison. • Note that this comparison is case sensitive. • 0 - if the two strings are equal • <0 - if string1 is less than string2 • >0 - if string1 is greater than string2 • Syntax:strncmp(string1,string2,length) • Eg
The strncasecmp() function • String comparison of the first n characters (case-insensitive) • This function returns: • 0 - if the two strings are equal • <0 - if string1 is less than string2 • >0 - if string1 is greater than string2 • Eg:<?php echo strncasecmp("Hello world!","hello earth!",6); ?>
The strrev()function reverses a string. • Syntax • strrev(string) • Eg:<?php echo strrev("Hello World"); ?> Res:dlroW olleH
The substr() function returns a part of a string. • Syntax • substr(string,start,length)string • Required. Specifies the string to return a part of • start • Required. Specifies where to start in the string • A positive number - Start at a specified position in the string • A negative number - Start at a specified position from the end of the string • 0 - Start at the first character in string • length • Optional. Specifies the length of the returned string. Default is to the end of the string. • A positive number - The length to be returned from the start parameter • Negative number - The length to be returned from the end of the string • eg
The trim() function removes whitespaces and other predefined characters from both sides of a string. • Syntax • trim(string,charlist) • string • Required. Specifies the string to check • charlist • Optional. Specifies which characters to remove from the string. If omitted, all of the following characters are removed: • "\0" - NULL • "\t" - tab • "\n" - new line • "\x0B" - vertical tab • "\r" - carriage return • " " - ordinary white space • eg
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. • Syntax • strstr(string,search) • 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 • eg
The str_replace() function replaces some characters with some other characters in a string. • Syntax • str_replace(find,replace,string,count) • find • Required. Specifies the value to find • replace • Required. Specifies the value to replace the value in find • string • Required. Specifies the string to be searched • count • Optional. A variable that counts the number of replacements • eg
The str_pad() function pads a string to a new length. • Syntax • str_pad(string,length,pad_string,pad_type) • string • Required. Specifies the string to pad • length • Required. Specifies the new string length. If this value is less than the original length of the string, nothing will be done • pad_string • Optional. Specifies the string to use for padding. Default is whitespace • pad_type • Optional. Specifies what side to pad the string. Possible values: • STR_PAD_BOTH • STR_PAD_LEFT • STR_PAD_RIGHT • Eg
The str_word_count() function counts the number of words in a string. • Syntax • str_word_count(string,return,char) • string • Required. Specifies the string to check • return • Optional. Specifies the return value of the str_word_count() function. Possible values: • 0 - Default. Returns the number of words found • 1 - Returns an array with the words from the string • 2 - Returns an array where the key is the position of the word in the string, and value is the actual word • char • Optional. Specifies special characters to be considered as words • eg