Prev | Current Page 332 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

We'll own this space in three months.
talk;
foreach($buzzwords as $bw) {
echo "The word $bw appears ".substr_count($talk,$bw)." time(s).
";
}
?>
This returns the following:
The word mindshare appears 1 time(s).
The word synergy appears 1 time(s).
The word space appears 2 time(s).
Replacing a Portion of a String with Another String
The substr_replace() function replaces a portion of a string with a replacement
string, beginning the substitution at a specified starting position and ending at a
predefined replacement length. Its prototype follows:
string substr_replace(string str, string replacement, int start [, int length])
Alternatively, the substitution will stop on the complete placement of replacement
in str. There are several behaviors you should keep in mind regarding the values of
start and length:
??? If start is positive, replacement will begin at character start.
??? If start is negative, replacement will begin at str length - start.
CHAPTER 9 ?–  S TRINGS AND REGULAR EXPRESS IONS 269
??? If length is provided and is positive, replacement will be length characters long.
??? If l
ength is provided and is negative, replacement will end at str length - length
characters.
Suppose you built an e-commerce site and within the user profile interface you
want to show just the last four digits of the provided credit card number. This function
is ideal for such a task:
$ccnumber = "1234567899991111";
echo substr_replace($ccnumber,"************",0,12);
?>
This returns the following:
************1111
Padding and Stripping a String
For formatting reasons, you sometimes need to modify the string length via either
padding or stripping characters.


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