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

Retrieving Part 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

Thestrstr()function returns the remainder of a string beginning with the first occurrence of a predefined string. Its prototype follows:

string strstr(string str, string occurrence)

This example uses the function in conjunction with theltrim()function to retrieve the domain name of an e-mail address:

<?php
   
$url = sales@example.com;
    echo ltrim(strstr($url, "@"),"@");
?>

This returns the following:

--------------------------------------------
example.com
--------------------------------------------

Returning Part of a String Based on Predefined Offsets

Thesubstr()function returns the part of a string located between a predefined starting offset and length positions. Its prototype follows:

string substr(string str, int start [, int length])

If the optionallengthparameter is not specified, the substring is considered to be the string starting atstartand ending at the end ofstr. There are four points to keep in mind when using this function:

  1. Ifstartis positive, the returned string will begin at thestartposition of the string. 
     
  2. Ifstartis negative, the returned string will begin at thelength-startposition of the string. 
     
  3. Iflengthis provided and is positive, the returned string will consist of the characters betweenstartandstart+length. If this distance surpasses the total string length, only the string betweenstartand the string’s end will be returned. 
     
  4. Iflengthis provided and is negative, the returned string will endlengthcharacters from the end ofstr.

Keep in mind thatstartis the offset from the first character ofstr; therefore, the returned string will actually start at character positionstart+ 1. Consider a basic example:

<?php
   
$car = "1944 Ford";
   
echo substr($car, 5);
?>

This returns the following:

--------------------------------------------
Ford
--------------------------------------------

The following example uses thelengthparameter:

<?php
    $car = "1944 Ford";
    echo substr($car, 0, 4);
?>

This returns the following:

--------------------------------------------
1944
--------------------------------------------

The final example uses a negativelengthparameter:

<?php
   
$car = "1944 Ford";
   
$yr = echo substr($car, 2, -5);
?>

This returns the following:

--------------------------------------------
44
--------------------------------------------



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