145Chapter 8StringsBoth the start-position argument and the length (Http web server)

145Chapter 8StringsBoth the start-position argument and the length argument can be negative, and in each casethe negativity has a different meaning. If the start-position is negative, it means that the start- ing character is determined by counting backward from the end of the string, rather than for- ward from the beginning. (A start position of 1means start with the last character, 2meanssecond-to-last, and so on.) Now, you might expect that a negative length would similarly imply that the substring should be determined by counting backward from the start character rather than forward. This is notthe case it is always true that the character at the start-position is the first character in thereturned string (not the last). Instead, a negative-length argument means that the final characteris determined by counting backward from the end rather than forward from the start position. Here are some examples, with positive and negative arguments: $alphabet_test = abcdefghijklmnop ; print( 3: . substr($alphabet_test, 3) .
); print( -3: . substr($alphabet_test, -3) .
); print( 3, 5: . substr($alphabet_test, 3, 5) .
); print( 3, -5: . substr($alphabet_test, 3, -5) .
); print( -3, -5: . substr($alphabet_test, -3, -5) .
); print( -3, 5: . substr($alphabet_test, -3, 5) .
); This gives us the output: 3: defghijklmnop-3: nop3, 5: defgh3, -5: defghijk-3, -5: -3, 5: nopIn the substr()example with a start position of 3and a length of 5, the ending positionis before the starting position, which in a sense specifies a string with negative length. The manual at www.php.net/manualcurrently says that such negative length calls to substr()will result in returning a string containing the single character at the start position. Instead, as in the preceding example, PHP5 seems to return empty strings in such cases. Caveat coder. Notice that there is an intimate relationship between the functions substr(),strstr(), andstrpos(). The substr()function selects a substring by numerical position, strstr() selects a substring by its content, and strpos()finds the numerical position of a given sub- string. In the case where we re sure in advance that the string $containinghas the string$containedas a substring, the expression: strstr($containing, $contained) should be equivalent to the code: substr($containing, strpos($containing, $contained)) String cleanup functionsAlthough they are technically substring functions, just like the others in this chapter, thefunctions chop(), ltrim(), and trim() are really used for cleaning up untidy strings. Theytrim whitespace off of the end, beginning, and beginning-and-end, respectively, of their singlestring argument. Some examples: Caution10
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Leave a Reply