taking part of a string?
Posted: Sun Nov 09, 2008 6:16 pm
I found this could but dont really understand it. Will this search a string and then put part of it in a variable? From what your searching for till another part or does anyone know of a way to do that?... Say this was a string "hello 5there php5 people". If i wanted to search from the first instance of 5 to the second. Thanks for your time and any ideas.
Code: Select all
<?php
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:
<?php
$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;
}
?>