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

(singke) #1

$translated = ".?";


// turn sincerity into sarcasm
print(strtr($text, $original, $translated));
?>


string substr_replace(string text, string replacement, integer
start, integer length)


Use substr_replace to replace one substring with another. Unlike str_replace,
which searches for matches, substr_replace simply removes a length of text and
inserts the replacement argument. The arguments operate similarly to substr. The
start argument is an index into the text argument with the first character numbered
as zero. If start is negative, counting will begin at the last character of the text
argument instead of the first.


The number of characters replaced is determined by the optional length argument or
the ends of the string. If length is negative, the returned string will end as 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 removed.


<?


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


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


string trim(string text)


The trim function strips whitespace from both the beginning and end of a string.
Compare this function to ltrim and chop. Whitespace includes spaces, tabs and other
nonprintable characters, including nulls (ASCII 0).


<?


$text = " whitespace ";
print(" \" ". trim($text). "\" ");
?>

Free download pdf