Home arrow PHP arrow Page 7 - String Theory

Working The Web - 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
PHP also comes with a bunch of functions constructed specially for Web development. The first of these is the very cool addslashes() function, which automatically escapes special characters in strings. You should make it a point to run this function on your variables prior to inserting them into a database (or any other application that has trouble with special characters).

<? $str = <<<EOF When Nicholas "Oz" Oseransky (Matthew Perry) goes over to introduce himself to his new neighbour, he's surprised to recognize notorious mob hit-man, Jimmy "The Tulip" Tudeski (Bruce Willis), currently in hiding after squealing on the Gogolack mob "family" in Chicago. EOF; // returns a string with all special characters escaped /* When Nicholas \"Oz\" Oseransky (Matthew Perry) goes over to introduce himself to his new neighbour, he\'s surprised to recognize notorious mob hit-man, Jimmy \"The Tulip\" Tudeski (Bruce Willis), currently in hiding after squealing on the Gogolack mob \"family\" in Chicago. */ echo addslashes($str); ?>
You can reverse the process with the stripslashes() function, which removes all the backslashes and returns a "clean" string.

The htmlentities() and htmlspecialchars() functions automatically convert special symbols (like < and >) into their corresponding HTML representations (< and >). Similarly, the nl2br() function automatically replaces blank lines in a string with the corresponding HTML line break tag
.

<? $str = "if (x < 5 && y > 8) \n { \n self_destruct() \n } \n"; // returns "if (x < 5 && y > 8) <br> { <br> self_destruct() <br> } <br>" echo nl2br(htmlentities($str)); ?>
The strip_tags() functions works in the opposite manner, finding and removing all HTML and PHP tags that may be embedded within the string.

<? $str = "<table><tr><td>Name</td><td>Price</td></tr></table>"; // returns "NamePrice" echo strip_tags($str); ?>
And that's about all I have. I hope you enjoyed this article, and that it offered you some insight into the massive amount of string processing power at your disposal in PHP4.

For more information on any of the functions listed here, take a look at the PHP manual page on strings at http://www.php.net/manual/en/ref.strings.php ...and until next time, stay healthy!

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

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 



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

Dev Shed Tutorial Topics: