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

Calculating the Similarity Between Two Strings - 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

Thestrspn()function returns the length of the first segment in a string containing characters also found in another string. Its prototype follows:

int strspn(string str1, string str2)

Here’s how you might usestrspn()to ensure that a password does not consist solely of numbers:

<?php
   
$password = "3312345";
   
if (strspn($password, "1234567890") == strlen($password))
       
echo "The password cannot consist solely of numbers!";
?>

In this case, the error message is returned because$passworddoes indeed consist solely of digits.

Calculating the Difference Between Two Strings

Thestrcspn()function returns the length of the first segment of a string containing characters not found in another string. Its prototype follows:

int strcspn(string str1, string str2)

Here’s an example of password validation usingstrcspn():

<?php
   
$password = "a12345";
   
if (strcspn($password, "1234567890") == 0) {
       
echo "Password cannot consist solely of numbers!";
    }
?>

In this case, the error message will not be displayed because$passworddoes not consist solely of numbers.

Manipulating String Case

Four functions are available to aid you in manipulating the case of characters in a string: strtolower(), strtoupper(), ucfirst(), and ucwords(). These functions are discussed in this section.

Converting a String to All Lowercase

Thestrtolower()function converts a string to all lowercase letters, returning the modified string. Nonalphabetical characters are not affected. Its prototype follows:

string strtolower(string str)

The following example usesstrtolower()to convert a URL to all lowercase letters:

<?php
    $url = http://WWW.EXAMPLE.COM/;
    echo strtolower($url);
?>

This returns the following:

--------------------------------------------
http://www.example.com/

--------------------------------------------

Converting a String to All Uppercase

Just as you can convert a string to lowercase, you can convert it to uppercase. This is accomplished with the functionstrtoupper(). Its prototype follows:

string strtoupper(string str)

Nonalphabetical characters are not affected. This example usesstrtoupper()to convert a string to all uppercase letters:

<?php
    $msg = "I annoy people by capitalizing e-mail text.";
    echo strtoupper($msg);
?>

This returns the following:

--------------------------------------------
I ANNOY PEOPLE BY CAPITALIZING E-MAIL TEXT.

--------------------------------------------

Capitalizing the First Letter of a String

Theucfirst()function capitalizes the first letter of the stringstr, if it is alphabetical. Its prototype follows:

string ucfirst(string str)

Nonalphabetical characters will not be affected. Additionally, any capitalized characters found in the string will be left untouched. Consider this example:

<?php
    $sentence = "the newest version of PHP was released today!";
    echo ucfirst($sentence);
?>

This returns the following:

--------------------------------------------
The newest version of PHP was released today!
--------------------------------------------

Note that while the first letter is indeed capitalized, the capitalized word PHP was left untouched.

Capitalizing Each Word in a String

Theucwords()function capitalizes the first letter of each word in a string. Its prototype follows:

string ucwords(string str)

Nonalphabetical characters are not affected. This example usesucwords()to capitalize each word in a string:

<?php
   
$title = "O'Malley wins the heavyweight championship!";
   
echo ucwords($title);
?>

This returns the following:

--------------------------------------------
O'Malley Wins The Heavyweight Championship!
--------------------------------------------

Note that if O’Malley was accidentally written as O’malley,ucwords()would not catch the error, as it considers a word to be defined as a string of characters separated from other entities in the string by a blank space on each side.

Please check back next week for the continuation of this article.



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