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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT