A great deal of programming involves working with strings of characters rather than numerical values. PHP includes a large number of functions for accessing, comparing, and manipulating strings.
(string)chr((int) $ASCII) (int)ord((string) $s)
chr() and ord() are complementary functions. chr() returns the single-character string corresponding to the$ASCIIvalue.ord() returns the base-10 ASCII value of thefirst character of$s. Appendix 3 contains a list of the 256 standard ASCII codes (base 10, 0-255) and their character representations for Windows computers. The lowercase alphabet starts at ASCII (base-10) 97 and the uppercase alphabet starts at ASCII 65. Nearly all ASCII characters can be displayed and printed by using their ASCII codes.
(mixed)count_chars((string) $s[,(int) $mode])
Counts the number of occurrences of every byte (with ASCII value 0…255) in $sand returns it according to$mode:
8.1 File Handling and Input/Output Functions 161
0–the default value, returns an array with the byte value as its keys and the number of occurrences of every byte as its values.
1–same as 0, but only byte values that actually occur in the string are listed.
2– same as 0, but only byte values that do not occur are listed.
3– a string containing all unique characters is returned.
4–a string containing all characters not appearing in$sis returned.
(string)ltrim ((string) $s[,(string)$charlist ]) (string)rtrim ((string) $s[,(string)$charlist ]) (string)trim ((string) $s[,(string)$charlist ])
Without the optional list of characters, strips whitespace characters from the left, right, or both left and right ends of a character string.
A list of other characters to be trimmed can be specified with the optional $charlist parameter. These functions are useful for removing blank characters and return/linefeed characters from strings.
Document 8.4 gives examples using the trim andexplode()functions.
Document 8.4(splitString.php)
<?php
$str="x x Mississippi x x";
echo ltrim($str,"x ")."<br />";
echo rtrim($str,"x ")."<br />";
echo trim($str,"x ")."<br />";
$A=array();
$A=explode(' ',$str);
var_dump($A);
?>
(int)strcasecmp((string) $s1,(string) $s2)
Performs a case-insensitive comparison of $s1 and $s2. strcasecmp()returns 0 if$s1and$s2are identical, an integer less than 0 if $s1 is less than $s2 (in the lexical sense), and an integer value greater than 0 if$s1is greater than$s2.
(int)strcmp((string) $s1,(string) $s2)
Performs a case-sensitive comparison of$s1and$s2.strcmp() returns 0 if$s1and$s2are identical, an integer value less than 0 if
$s1is less than$s2(in the lexical sense), and an integer value greater than 0 if$s1is greater than$s2.
(string) stristr ((string)$s,(mixed)$lookFor)
Returns all of$sfrom thefirst occurrence of$lookForto the end of$s. If$lookFor is not found, returns false. The search is case- insensitive. If $lookFor is not a string, it is converted to an integer and interpreted as the ordinal value of a character.
(int)strlen((string) $s);
Returns the length (number of characters) in $s. (int)strncasecmp((string) $s1,(string) $s2,
(int) $n_char)
Performs a case-insensitive comparison on the first $n_char characters of$s1and$s2. strncasecmp()returns 0 if$s1and$s2are identical, an integer value less than 0 if$s1is less than$s2(in the lexical sense), and an integer value greater than 0 if$s1is greater than$s2. (int)strncmp((string) $s1,(string) $s2,(int) $n_char)
Performs a case-sensitive comparison on the first$n_char char- acters of$s1and$s2.strncmp()returns 0 if$s1and$s2are iden- tical, an integer value less than 0 if$s1is less than$s2(in the lexical sense), and an integer value greater than 0 if$s1is greater than$s2. (int)strpos((string) $s,(mixed) $lookFor
[,(int) $offset])
Returns the numeric position of thefirst occurrence of$lookForin
$s. If the optional$offsetparameter is provided (default is 0), the search starts at the specified offset position rather than at the beginning of$s. (string)strtolower((string) $s)
(string)strtoupper((string) $s)
strtolower() converts the alphabetic characters in$s to low- ercase. strtoupper() converts the alphabetic characters in $s to uppercase.
8.2 String Handling Functions 163
(string)substr((string) $s,(int) $start [,(int) $length])
Returns$length characters of $s, starting at$start. The first character in a string is at position 0. If the length of$sis less than or equal to$startcharacters long, a warning message will be displayed.
If$length is not specified, all the characters from position $start will be returned.
(int)substr_compare((string) $s1,
(string) $s2,(int) $offset[,(int) $length [,(bool) $case_insensitivity]]
Returns 0 if$s1is equal to$s2, <0 if$1is less than$s2, and >0 if $s1 is greater than $s2. If the optional $length parameter is supplied (default is 0), the comparison uses $length characters of
$s1. If $length is greater than or equal to the length of $s1, a warning message will be displayed. If the optional$offsetparameter is specified (default is 0), the comparision starts at the specified offset from the beginning of $s1. If $offset is negative, the comparison starts counting from the end of the string. If the optional
$case_insensitivityparameter is given a value oftrue(default isfalse), the comparison is case-insensitive.
(int)substr_count((string) $s,(string) $what [,(int) $offset[, $length]])
Returns the number of times the string $what occurs in $s, optionally starting at $offset (default is 0) and including the next
$lengthcharacters.
Document 8.5 shows output from some of the string functions.
Document 8.5(stringCompare.php)
<?php
echo strcasecmp("Dave","David")."<br />"; // returns -4 echo strcasecmp("DAVID","david")."<br />"; // returns 0 echo strcmp("david","DAVID")."<br />"; // returns 1 echo strcmp("Dave","David")."<br />"; // returns -1 echo strcmp("DAVID","david")."<br />"; // returns -1 echo strcmp("david","DAVID")."<br />"; // returns 1
$len=min(strlen("Dave"),strlen("David"));
echo strncasecmp("Dave","David",$len)."<br />";// returns -4 echo strncmp("Dave","David", 3)."<br />"; // returns 0
echo stristr("David",'v')."<br />"; //returns vid echo strpos("David",'i')."<br />"; // returns 3 echo strtolower("David")."<br />"; // returns david echo strtoupper("David")."<br />"; // returns DAVID echo substr("David",3)."<br />"; // returns id echo substr_compare("Mississippi","Missouri",0,5)."<br />";
// returns -1 echo substr_count("Mississippi","ss"); // returns 2
?>