Prev | Current Page 330 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

If the
optional parameter count is defined, only count occurrences found in str will be
replaced.
This function is ideal for hiding e-mail addresses from automated e-mail address
retrieval programs:
$author = "jason@example.com";
$author = str_replace("@","(at)",$author);
echo "Contact the author of this article at $author.";
?>
This returns the following:
Contact the author of this article at jason(at)example.com.
The function str_ireplace() operates identically to str_replace(), except that it
is capable of executing a case-insensitive search.
Retrieving Part of a String
The strstr() function returns the remainder of a string beginning with the first
occurrence of a predefined string. Its prototype follows:
string strstr(string str, string occurrence)
266 CHAPTER 9 ?–  STRINGS AND REGULAR EXPRESSIONS
This example uses the function in conjunction with the ltrim() function to
retrieve the domain name of an e-mail address:
$url = "sales@example.com";
echo ltrim(strstr($url, "@"),"@");
?>
This returns the following:
example.com
Returning Part of a String Based on Predefined Offsets
The substr() function returns the part of a string located between a predefined
starting offset and length positions. Its prototype follows:
string substr(string str, int start [, int length])
If the optional length parameter is not specified, the substring is considered to be
the string starting at start and ending at the end of str.


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