PHP
  Home arrow PHP arrow Page 3 - String Theory
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

String Theory
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2001-09-20

    Table of Contents:
  • String Theory
  • Secret Agent Man
  • Running Backwards
  • Getting Into Position
  • Instant Paralysis
  • A Quick Trim
  • Working The Web

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    String Theory - Running Backwards


    (Page 3 of 7 )

    With the basics out of the way, let's now turn to some of the other string functions available in PHP. Other than echo() and print(), the three functions you're likely to encounter most often are strlen(), explode() and implode().

    The strlen() function returns the length of a particular string, and can come in handy for operations which involve processing every character in a string.

    <? $str = "The wild blue fox jumped over the gigantic yellow pumpkin"; // returns 57 echo strlen($str); ?>
    The explode() function splits a string into smaller components on the basis of a user-specified pattern, and then returns these elements as an array.

    <? $str = "I'm not as think as you stoned I am"; // split into individual words and store in $words[] $words = explode(" ", $str); ?>
    This function is particularly handy if you need to take a string containing a list of items (for example, a comma-delimited list) and separate each element of the list for further processing. Here's an example:

    <? $str = "Rachel, Monica, Phoebe, Joey, Chandler, Ross"; // split into array $arr = explode(", ", $str); // display as list echo "<ul>"; foreach($arr as $friend) { echo "<li>$friend"; } echo "</ul>"; ?>
    Obviously, you can also do the reverse - the implode() function creates a single string from all the elements of an array, joining them together with a user-defined separator. Reversing the example above, we have:

    <? $arr = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"); // create string from array $str = implode(" and ", $arr); // returns "Rachel and Monica and Phoebe and Joey and Chandler and Ross are friends" echo "$str are friends"; ?>
    The chr() and ord() functions come in handy when converting from ASCII codes to characters and vice-versa. For example,

    <? // returns "A" print chr(65); // returns 97 print ord("a"); ?>
    In case you need to repeat a string, PHP offers the str_repeat() function, which accepts two arguments - the string to be repeated, and the number of times to repeat it. Here's an example:

    <? // returns "Play it again, Sam!" eight times echo str_repeat("Play it again, Sam!", 8); ?>
    And if you ever find the need to reverse a string, well, you can always reach for the strrev() function...

    <? $str = "Wassup, dood?"; // reverse string // $rts now contains ?dood ,pussaW $rts = strrev($str); echo "Sorry, you seem to be talking backwards - what does $rts mean?"; ?>
    I couldn't have put it better myself!{mospagebreak title=Of Jumping Cows And Purple Pumpkins} Next up, the substr() function. As the name implies, this is the function that allows you to slice and dice strings into smaller strings. Here's what it looks like:

    substr(string, start, length)
    where "string" is a string or string variable, "start" is the position to begin slicing at, and "length" is the number of characters to return from "start".

    Here's an example which demonstrates how this works:

    <? $str = "The cow jumped over the moon, giggling madly as a purple pumpkin with fat ears exploded into confetti"; // returns "purple pumpkin with fat ears" echo substr($str, 50, 28); ?>
    You can use this function to split a string into smaller chunks of a fixed size,

    <? $str = "The cow jumped over the moon, giggling madly as a purple pumpkin with fat ears exploded into confetti"; $count = 0; // length of chunk $size = 11; /* returns The cow jum ped over th e moon, gig gling madly as a purpl e pumpkin w ith fat ear s exploded into confet ti */ while (($size*$count) < strlen($str)) { $temp = substr($str, ($size*$count), $size); $count++; echo "$temp \n"; } ?>
    or you could take the easy way out and use the built-in chunk_split() function, designed specifically for this purpose.

    <? $str = "The cow jumped over the moon, giggling madly as a purple pumpkin with fat ears exploded into confetti"; // this is equivalent to the previous example echo chunk_split($str, 11); ?>
    You can also use the substr() function to extract a particular character from a string,

    <? $str = "The cow jumped over the moon, giggling madly as a purple pumpkin with fat ears exploded into confetti"; // returns "j" echo substr($str, 8, 1); ?>
    or you can use one of PHP4's cool new features and access a character by specifying its position in the string within curly braces (remember that the first character equates to position 0).

    <? $str = "The cow jumped over the moon, giggling madly as a purple pumpkin with fat ears exploded into confetti"; // returns "j" echo $str{8}; ?>

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


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway