Home arrow PHP arrow Page 4 - Parsing Strings and Regular Expressions

Determining the Frequency of a String’s Appearance - PHP

In this fourth part of a five-part series on strings and regular expressions in PHP, you'll learn how to perform complex string parsing, find the last occurrence of a string, and more. 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. Parsing Strings and Regular Expressions
  2. Finding the Last Occurrence of a String
  3. Retrieving Part of a String
  4. Determining the Frequency of a String’s Appearance
By: Apress Publishing
Rating: starstarstarstarstar / 1
July 08, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Thesubstr_count()function returns the number of times one string occurs within another. Its prototype follows:

int substr_count(string str, string substring)

The following example determines the number of times an IT consultant uses various buzzwords in his presentation:

<?php
    $buzzwords = array("mindshare", "synergy", "space");

    $talk = <<< talk
    I'm certain that we could dominate mindshare in this space with
    our new product, establishing a true synergy between the marketing
    and product development teams. We'll own this space in three months.
talk;

    foreach($buzzwords as $bw) {
        echo "The word $bw appears ".substr_count($talk,$bw)." time(s).<br />";
    }
?>

This returns the following:

--------------------------------------------
The word mindshare appears 1 time(s).
The word synergy appears 1 time(s).
The word space appears 2 time(s).
--------------------------------------------

Replacing a Portion of a String with Another String

Thesubstr_replace()function replaces a portion of a string with a replacement string, beginning the substitution at a specified starting position and ending at a predefined replacement length. Its prototype follows:

string substr_replace(string str, string replacement, int start [, int length])

Alternatively, the substitution will stop on the complete placement ofreplacementinstr. There are several behaviors you should keep in mind regarding the values ofstartandlength:

  1. Ifstartis positive,replacementwill begin at characterstart
     
  2. Ifstartis negative,replacementwill begin atstrlength -start
     
  3. Iflengthis provided and is positive,replacementwill belengthcharacters long. 
     
  4. Iflengthis provided and is negative,replacementwill end atstrlength -lengthcharacters.

Suppose you built an e-commerce site and within the user profile interface you want to show just the last four digits of the provided credit card number. This function is ideal for such a task:

<?php
   
$ccnumber = "1234567899991111";
   
echo substr_replace($ccnumber,"************",0,12);
?>

This returns the following:

--------------------------------------------
************1111
--------------------------------------------

Please check back next week for the conclusion to 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: