PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Getting Into Position
    ( Page 4 of 7 )

    You can use the case-sensitive strpos() function to locate the first occurrence of a character in a string,

    <? $str = "Robin Hood and his band of merry men"; // returns 0 echo strpos($str, "R"); ?>
    and the strrpos() function to locate its last occurrence.

    <? $str = "Robin Hood and his band of merry men"; // returns 33 echo strrpos($str, "m"); ?>
    The substr_count() function comes in handy if you need to know how many times a specific patter recurs in a string.

    <? $str = "'tis said that the is the most common word in the English language, and e is the most common letter"; // returns 4 echo substr_count($str, "the"); ?>
    The strstr() function scans a string for a particular pattern and returns the contents of that string from the point onwards (for a case-insensitive version, try stristr()).

    <? $str = "As Mulder keeps saying, the truth is out there"; // returns "the truth is out there" echo strstr($str, "the"); ?>
    If you need to compare two strings, the strcmp() function performs a case-sensitive binary comparison of two strings, returning a negative value if the first is "less" than the second, a positive value if it's the other ways around, and zero if both strings are "equal". Take a look at a couple of examples to see what this means:

    <? // returns -1 because a < s echo strcmp("apple", "strawberry"); // returns 1 because u > p (s == s) echo strcmp("superman", "spiderman"); // returns 0 echo strcmp("ape", "ape"); ?>
    You can perform a case-insensitive comparison with the strcasecmp() function, or adopt a different approach with the very cool "natural order" comparison, which compares strings the way humans (rather than computers) would.

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

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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 ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek