com PHP series,
I discuss the many improvements and additions to
PHP 5's object-oriented architecture.
summary;
$words = sizeof(explode(' ',strip_tags($summary)));
echo "Total words in summary: $words";
?>
This returns the following:
Total words in summary: 22
The explode() function will always be considerably faster than preg_split(), split(),
and spliti(). Therefore, always use it instead of the others when a regular expression
isn??™t necessary.
?– Note You might be wondering why the previous code is indented in an inconsistent manner. The
multiple-line string was delimited using heredoc syntax, which requires the closing identifier to not be
indented even a single space. Why this restriction is in place is somewhat of a mystery, although one
would presume it makes the PHP engine??™s job a tad easier when parsing the multiple-line string. See
Chapter 3 for more information about heredoc.
Converting an Array into a String
Just as you can use the explode() function to divide a delimited string into various
array elements, you concatenate array elements to form a single delimited string
using the implode() function. Its prototype follows:
string implode(string delimiter, array pieces)
This example forms a string out of the elements of an array:
$cities = array("Columbus", "Akron", "Cleveland", "Cincinnati");
echo implode("|", $cities);
?>
CHAPTER 9 ?– S TRINGS AND REGULAR EXPRESS IONS 263
This returns the following:
Columbus|Akron|Cleveland|Cincinnati
Performing Complex String Parsing
The strpos() function finds the position of the first case-sensitive occurrence of
substr in a string.
Pages:
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339