Prev | Current Page 329 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Its prototype follows:
int strrpos(string str, char substr [, offset])
The optional parameter offset determines the position from which strrpos() will
begin searching. Suppose you wanted to pare down lengthy news summaries, truncating
the summary and replacing the truncated component with an ellipsis. However,
rather than simply cut off the summary explicitly at the desired length, you want it to
operate in a user-friendly fashion, truncating at the end of the word closest to the truncation
length. This function is ideal for such a task. Consider this example:
// Limit $summary to how many characters?
$limit = 100;
$summary = <<< summary
In the latest installment of the ongoing Developer.com PHP series,
I discuss the many improvements and additions to
PHP 5's object-oriented
architecture.
summary;
if (strlen($summary) > $limit)
$summary = substr($summary, 0, strrpos(substr($summary, 0, $limit),
' ')) . '...';
echo $summary;
?>
CHAPTER 9 ?–  S TRINGS AND REGULAR EXPRESS IONS 265
This returns the following:
In the latest installment of the ongoing Developer.com PHP series,
I discuss the many...
Replacing All Instances of a String with Another String
The str_replace() function case sensitively replaces all instances of a string with
another. Its prototype follows:
mixed str_replace(string occurrence, mixed replacement, mixed str [, int count])
If occurrence is not found in str, the original string is returned unmodified.


Pages:
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341