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  
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 - 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

    - 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 3 hosted by Hostway