Day 8 – String Theory

Zend PHP Certification

Eight days in and I’m still only dealing with the basics.  Today is all about Strings – Comparing, Searching and Replacing data.  There are a good amount of tricks in here, especially when converting strings to numbers and vice-versa.  Time to pay attention and take notes!


Comparison

  • When doing a string comparison to a number, be aware that PHP will attempt to convert the string to a number.  This is done by reading character by character until it cannot equal a number.  For example: "123abc" = 123 and "0×23bo" = 0×23b.  If PHP cannot figure something out, it equates as 0: "abc" = 0.
  • This conversion can be avoided by using the triple-equal sign (===) to force type comparison first.
  • String comparison is also possible with two functions: strcmp($str1, $str2) and strcasecmp($str1, $str2).  These are identical in operation with the exception of the second function ignores case.  Both will return 0 indicating the strings are equal, <0 if $str1 is alphabetically less than $str2, >0 if $str1 is alphabetically higher than $str2.
  • Additionally, you can use strncasecmp($str1, $str2, $len) to compare first $len characters of case-insensitive strings.

Searching

  • When searching strings, there are usually two types of functions.  Simple (fast) and Complex (slow).  As an example, to search a string for characters, you can quickly use strpos($string, $word).  It’s fast and will return either false (check using ===) if not found or the starting position of that $word.
  • There is, conversely, a more complex version, strstr($string, $word) which returns the text from that point on.
  • Both functions have optional parameters to tell where to start looking, strpos($string, $word, $starting).
  • And there are case-insensitive versions of both: stripos and stristr.
  • While the previous functions were basic searching, there is another type called "masking."  This searches a string for the largest run of characters matching a specified set (whitelist) or outside a specified set (blacklist).  The strspn($string, $mask) function performs the whitelist version while the strcspn($string, $mask) is the blacklist one.
  • Both have two optional parameters to specify where to start searching and how many characters to look at.
  • There is a HUGE section (tomorrow) on Regular Expressions, so this isn’t it (don’t worry).

Replacing

  • The basic find and replace function is str_replace($from, $to, $string). 
    Personal Note: it is really annoying how often function parameters are rearranged between similar functions.
  • This function can take (alternately) arrays instead of strings for $from and $to.
  • There is also a case-insensitive version (what a surprise) called str_ireplace().
  • To replace text from a certain point in a string, you can use substr_replace($string, $replacement, $start, [$length]) with $length being an optional parameter limiting the amount that can be changed.
  • Finally there is substr($string, $start, [$len]), designed to extract a portion of text from $string starting at $string with optional $len length.  This has a very useful feature: $start values that are negative will be the length of the string – $start.

I can’t read/write that much more tonight, too busy!  I need to do this stuff earlier in the day (ugh, I need more free time).  Tomorrow is string formatting and regular expressions (woof).

No Comments

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  • Donate

    If my work has helped you and you want to return the favor, you could purchase something for me from my Amazon Wish List or send me a donation via PayPal.

  • My Lifestream

  • License

    Unless otherwise noted, all source code and compiled files published on this website are released under the terms of the GNU Lesser General Public License.