Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Search and Replacement
 
Article Index
Search and Replacement
Page 2
Page 3
Page 4

 

may result in unexpected problems. For example, the following script will incorrectly report that the string "THRee" cannot be found inside the string "THRee merry men":

<?php

$haystack = 'Three merry men';

$pos = strpos ($haystack, 'Three');

if (!$pos)
echo "String not found\n";
else
echo "String found at position $pos\n";

?>

 

Although strpos() performs its search left-to-right, it is possible to start searching from the end of a string and move backward using the strrpos function. Unlike strpos(), however, strrpos() is able to search for only one character. If you specify a string with more than one character as the $needle parameter, only the first character will be considered.

As you can imagine, strpos() is case sensitive, so that, for example, it wouldn't have been able to find the word "three" in the preceding example.

Interestingly, there is no non-case-sensitive alternative to strpos(). However, PHP provides the strstr function, which offers a functionality that is similar to strpos() and provides a non-case-sensitive variant called stristr().

Unlike strpos(), strstr() actually returns the portion of $haystack that succeeds $needle. The following script, for example, will return String found: merry men:


Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Your Ad Here
Natural 406
Statistic


Last Post

 
Top! Top!