note 85835 added to function.strpos
| From: | mickRacky at osu1 dot php dot net | Date: | Fri, 19 Sep 2008 18:17:45 +0000 |
| Subject: | note 85835 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144556@lists.php.net to get a copy of this message | ||
here's a php implementation of stdc++ string class find_first_of using strpos.
function find_first_of($haystack, $needlesAsString, $offset=0)
{
$max = strlen($needlesAsString);
$index = strlen($haystack)+1;
for($ii=0; $ii<$max;$ii++){
$result = strpos($haystack,$needlesAsString[$ii], $offset);
if( $result !== FALSE && $result < $index)
$index = $result;
}
return ( $index > strlen($haystack)? FALSE: $index);
}
Example:
$test="Ralph: One of these days, Alice!!";
$look_for=":!,"; // punctuation marks
$ss = 0;
while( $answer=find_first_of($test,$look_for,$ss) ) {
echo $answer . "\n";
$ss = $answer+1;
}
This prints out:
5
24
31
32
----
Server IP: 64.71.164.2
Probable Submitter: 64.157.94.60
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/85835
Del: integrated -- https://master.php.net/note/delete/85835/integrated
Del: useless -- https://master.php.net/note/delete/85835/useless
Del: bad code -- https://master.php.net/note/delete/85835/bad+code
Del: spam -- https://master.php.net/note/delete/85835/spam
Del: non-english -- https://master.php.net/note/delete/85835/non-english
Del: in docs -- https://master.php.net/note/delete/85835/in+docs
Del: other reasons-- https://master.php.net/note/delete/85835
Reject -- https://master.php.net/note/reject/85835
Search -- https://master.php.net/manage/user-notes.php