Home arrow PHP arrow Page 3 - Functions of Strings and Regular Expressions

Other String-Specific Functions - PHP

In this second part of a five-part series on strings and regular expressions in PHP, you'll learn about regular expression functions and a variety of string-specific functions. This article is excerpted from chapter nine of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

TABLE OF CONTENTS:
  1. Functions of Strings and Regular Expressions
  2. Delimiting Special Regular Expression Characters
  3. Other String-Specific Functions
  4. Calculating the Similarity Between Two Strings
By: Apress Publishing
Rating: starstarstarstarstar / 2
June 24, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In addition to the regular expression–based functions discussed in the first half of this chapter, PHP offers more than 100 functions collectively capable of manipulating practically every imaginable aspect of a string. To introduce each function would be out of the scope of this book and would only repeat much of the information in the PHP documentation. This section is devoted to a categorical FAQ of sorts, focusing upon the string-related issues that seem to most frequently appear within community forums. The section is divided into the following topics:

  1. Determining string length 
     
  2. Comparing string length 
     
  3. Manipulating string case 
     
  4. Converting strings to and from HTML 
     
  5. Alternatives for regular expression functions 
     
  6. Padding and stripping a string 
     
  7. Counting characters and words

Determining the Length of a String

Determining string length is a repeated action within countless applications. The PHP function strlen() accomplishes this task quite nicely. This function returns the length of a string, where each character in the string is equivalent to one unit. Its prototype follows:

int strlen(string str)

The following example verifies whether a user password is of acceptable length:

<?php
   
$pswd = "secretpswd";
   
if (strlen($pswd) < 10)
        echo "Password is too short!";
    else
        echo "Password is valid!";
?>

In this case, the error message will not appear because the chosen password consists of ten characters, whereas the conditional expression validates whether the target string consists of less than ten characters.

Comparing Two Strings

String comparison is arguably one of the most important features of the string-handling capabilities of any language. Although there are many ways in which two strings can be compared for equality, PHP provides four functions for performing this task: strcmp(), strcasecmp(),strspn(), andstrcspn(). These functions are discussed in the following sections.

Comparing Two Strings Case Sensitively

Thestrcmp()function performs a binary-safe, case-sensitive comparison of two strings. Its prototype follows:

int strcmp(string str1, string str2)

It will return one of three possible values based on the comparison outcome:

  1. 0ifstr1andstr2are equal
  2. -1ifstr1is less thanstr2
  3. 1ifstr2is less thanstr1

Web sites often require a registering user to enter and then confirm a password, lessening the possibility of an incorrectly entered password as a result of a typing error.strcmp()is a great function for comparing the two password entries because passwords are often case sensitive:

<?php
   
$pswd = "supersecret";
   
$pswd2 = "supersecret2";

    if (strcmp($pswd,$pswd2) != 0)
        echo "Passwords do not match!";
    else
        echo "Passwords match!";
?>

Note that the strings must match exactly forstrcmp()to consider them equal. For example,Supersecretis different fromsupersecret. If you’re looking to compare two strings case insensitively, considerstrcasecmp(), introduced next.

Another common point of confusion regarding this function surrounds its behavior of returning0if the two strings are equal. This is different from executing a string comparison using the==operator, like so:

if ($str1 == $str2)

While both accomplish the same goal, which is to compare two strings, keep in mind that the values they return in doing so are different.

Comparing Two Strings Case Insensitively

Thestrcasecmp()function operates exactly likestrcmp(), except that its comparison is case insensitive. Its prototype follows:

int strcasecmp(string str1, string str2)

The following example compares two e-mail addresses, an ideal use forstrcasecmp()because case does not determine an e-mail address’s uniqueness:

<?php
   
$email1 = admin@example.com;
    $email2 = "ADMIN@example.com";

    if (! strcasecmp($email1, $email2))
        echo "The email addresses are identical!";
?>

In this example, the message is output becausestrcasecmp()performs a case-insensitive comparison of$email1and$email2and determines that they are indeed identical.



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: