Home arrow PHP arrow Page 5 - String Theory

Instant Paralysis - PHP

You may not know this, but the latest version of PHP comes with avery powerful set of string manipulatation tools. This article takes anin-depth look at these tools and illustrates how they can save you time andeffort in your daily development activities.

TABLE OF CONTENTS:
  1. String Theory
  2. Secret Agent Man
  3. Running Backwards
  4. Getting Into Position
  5. Instant Paralysis
  6. A Quick Trim
  7. Working The Web
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 13
September 20, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
You can also search for specific patterns in your strings with regular expressions, via PHP's preg_match() function.

<? $str = "johndoe@somedomain.com"; $pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/"; // returns true echo preg_match($pattern, $str); ?>
You can also use the ereg() function to perform a regular expression match,

<? $str = "yo ho ho and a bottle of gum"; // returns true echo ereg("bo*", $str); ?>
and the ereg_replace() function to perform a search-and-replace operation.

<? $str = "yo ho ho and a bottle of gum"; // returns "yo ho ho and a bottle of rum" echo ereg_replace("gum", "rum", $str); ?>
If you need to perform substitution within a string, PHP also has the str_replace() function, designed specifically to perform find-and-replace operations.

<? $str = "...as Michael dropped into a crouch and came up under Frank's punch, he swiveled to the side and kicked Frank in the spine, immediately breaking one of Frank's vertebrae and rendering him paralyzed for life..."; // returns "...as Michael dropped into a crouch and came up under John's punch, he swiveled to the side and kicked John in the spine, immediately breaking one of John's vertebrae and rendering him paralyzed for life..." echo str_replace("Frank", "John", $str); ?>


 
 
>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: