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

Finding the Last Occurrence of a String - 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

Thestrrpos()function finds the last occurrence of a string, returning its numerical position. Its prototype follows:

int strrpos(string str, char substr [, offset])

The optional parameteroffsetdetermines the position from whichstrrpos()will begin searching. Suppose you wanted to pare down lengthy news summaries, truncating the summary and replacing the truncated component with an ellipsis. However, rather than simply cut off the summary explicitly at the desired length, you want it to operate in a user-friendly fashion, truncating at the end of the word closest to the truncation length. This function is ideal for such a task. Consider this example:

<?php
    // Limit $summary to how many characters?
    $limit = 100;

    $summary = <<< summary
    In the latest installment of the ongoing Developer.com PHP series,
    I discuss the many improvements and additions to
    <a href="http://www.php.net">PHP 5's</a> object-oriented
    architecture.
summary;

    if (strlen($summary) > $limit)
        $summary = substr($summary, 0, strrpos(substr($summary, 0, $limit),
               ' ')) . '...';
    echo $summary;
?>

This returns the following:

--------------------------------------------
In the latest installment of the ongoing Developer.com PHP series,
I discuss the many...
--------------------------------------------

Replacing All Instances of a String with Another String

Thestr_replace()function case sensitively replaces all instances of a string with another. Its prototype follows:

mixed str_replace(string occurrence, mixed replacement, mixed str [, int count])

Ifoccurrenceis not found instr, the original string is returned unmodified. If the optional parametercount is defined, onlycount occurrences found instrwill be replaced.

This function is ideal for hiding e-mail addresses from automated e-mail address retrieval programs:

<?php
   
$author = jason@example.com;
    $author = str_replace("@","(at)",$author);
   
echo "Contact the author of this article at $author.";
?>

This returns the following:

--------------------------------------------
Contact the author of this article at jason(at)example.com.
--------------------------------------------

The functionstr_ireplace()operates identically tostr_replace(), except that it is capable of executing a case-insensitive search.



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

blog comments powered by Disqus
   

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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: