Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

string substr(string text, integer start, integer length)


Use the substr function to extract a substring from the text argument. A string is
returned that starts with the character identified by the start argument, counting from
zero. If start is negative, counting will begin at the last character of the text
argument instead of the first and work backward.


The number of characters returned is determined by the length argument or the
beginning and end of the string. If length is negative, the returned string will end that
many characters from the end of the string. In any case, if the combination of start and
length calls for a string of negative length, a single character is returned.


This function is safe for use with binary strings.


<?


$text = "My dog's name is Angus.";


//replace Angus with Gus
print(substr_replace($text, "Gus", 17, 5));
?>


Encoding and Decoding


The functions in this section transform data from one form to another.
This includes stripping certain characters, substituting some characters
for others, and translating data into some encoded form.


string addcslashes(string text, string characters)


The addcslashes function returns the text argument after escaping characters in the
style of the C programming language. Briefly, this means special characters are replaced
with codes, such as \n replacing a newline character, and other characters outside ASCII
32-126 are replaced with backslash octal codes.


The optional characters argument may contain a list of characters to be escaped,
which overrides the default of escaping all special characters. The characters are
specified with octal notation. You may specify a range using two periods as in the
example below.


<?


$s = addcslashes($s, "\0..\37");
?>

Free download pdf