note 102336 added to function.strpos
| From: | jaimthorn at yahoo dot com | Date: | Wed, 09 Feb 2011 10:09:20 +0000 |
| Subject: | note 102336 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176804@lists.php.net to get a copy of this message | ||
A complete set of strn*pos functions that look for the nth occurrence of the needle in the haystack.
Based on a version I posted earlier, below; this new version conforms more to the semantics of
strpos.
<?php
/**
* This function implements all the strn*pos functions, which return the $nth occurrence of $needle
* in $haystack, or false if it doesn't exist / when illegal parameters have been supplied.
*
* @param string $haystack the string to search in.
* @param MIXED $needle the string or the ASCII value of the character to search for.
* @param integer $nth the number of the occurrence to look for.
* @param integer $offset the position in $haystack to start looking for $needle.
* @param bool $insensitive should the function be case insensitive?
* @param bool $reverse should the function work its way backwards in the haystack?
* @return MIXED integer either the position of the $nth occurrence of $needle in
$haystack,
* or boolean false if it can't be found.
*/
function strnripos_generic( $haystack, $needle, $nth, $offset, $insensitive, $reverse )
{
// If needle is not a string, it is converted to an integer and applied as the ordinal value of
a character.
if( ! is_string( $needle ) ) {
$needle = chr( (int) $needle );
}
// Are the supplied values valid / reasonable?
$len = strlen( $needle );
if( 1 > $nth || 0 === $len ) {
return false;
}
if( $insensitive ) {
$haystack = strtolower( $haystack );
$needle = strtolower( $needle );
}
if( $reverse ) {
$haystack = strrev( $haystack );
$needle = strrev( $needle );
}
// $offset is incremented in the call to strpos, so make sure that the first
// call starts at the right position by initially decreasing $offset by $len.
$offset -= $len;
do
{
$offset = strpos( $haystack, $needle, $offset + $len );
} while( --$nth && false !== $offset );
return false === $offset || ! $reverse ? $offset : strlen( $haystack ) - $offset;
}
/**
* @see strnripos_generic
*/
function strnpos( $haystack, $needle, $nth, $offset = 0 )
{
return strnripos_generic( $haystack, $needle, $nth, $offset, false, false );
}
/**
* @see strnripos_generic
*/
function strnipos( $haystack, $needle, $nth, $offset = 0 )
{
return strnripos_generic( $haystack, $needle, $nth, $offset, true, false );
}
/**
* @see strnripos_generic
*/
function strnrpos( $haystack, $needle, $nth, $offset = 0 )
{
return strnripos_generic( $haystack, $needle, $nth, $offset, false, true );
}
/**
* @see strnripos_generic
*/
function strnripos( $haystack, $needle, $nth, $offset = 0 )
{
return strnripos_generic( $haystack, $needle, $nth, $offset, true, true );
}
$haystack = 'Dit is een HoTtentotTentenTentenToonstellingTest!';
echo strnpos ( $haystack, 't', 5 ), ' === ', strnpos ( $haystack, 116, 5 ),
PHP_EOL;
echo strnipos ( $haystack, 't', 5 ), ' === ', strnipos ( $haystack, 116, 5 ),
PHP_EOL;
echo strnrpos ( $haystack, 't', 5 ), ' === ', strnrpos ( $haystack, 116, 5 ),
PHP_EOL;
echo strnripos( $haystack, 't', 5 ), ' === ', strnripos( $haystack, 116, 5 ),
PHP_EOL;
echo PHP_EOL;
echo strnpos ( $haystack, 'T', 5 ), ' === ', strnpos ( $haystack, 84, 5 ),
PHP_EOL;
echo strnipos ( $haystack, 'T', 5 ), ' === ', strnipos ( $haystack, 84, 5 ),
PHP_EOL;
echo strnrpos ( $haystack, 'T', 5 ), ' === ', strnrpos ( $haystack, 84, 5 ),
PHP_EOL;
echo strnripos( $haystack, 'T', 5 ), ' === ', strnripos( $haystack, 84, 5 ),
PHP_EOL;
----
Server IP: 69.147.83.197
Probable Submitter: 62.195.52.58
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/102336
Del: integrated -- https://master.php.net/note/delete/102336/integrated
Del: useless -- https://master.php.net/note/delete/102336/useless
Del: bad code -- https://master.php.net/note/delete/102336/bad+code
Del: spam -- https://master.php.net/note/delete/102336/spam
Del: non-english -- https://master.php.net/note/delete/102336/non-english
Del: in docs -- https://master.php.net/note/delete/102336/in+docs
Del: other reasons-- https://master.php.net/note/delete/102336
Reject -- https://master.php.net/note/reject/102336
Search -- https://master.php.net/manage/user-notes.php