Here's the function I came up with- seems to work: //finds all occurrences of $char in $temp and returns char positions in an array function char_positions($temp, $char) { while (strpos($temp, $char) !== FALSE){ //note the two '==' $found = strpos($temp, $char); $result[] = $found; $temp = substr($temp, $found + 1, strlen($temp)); }
for ($i=0; $i<sizeof($result); $i++){ if ($i > 0){ #if not the first element $result[$i] = $result[$i] + $result[$i-1] + 1; } } if (sizeof($result)>0){ return FALSE; }else{ return $result; } } David ----- Original Message ----- From: "Kevin Stone" <[EMAIL PROTECTED]> To: "David Yee" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Thursday, January 03, 2002 12:30 PM Subject: Re: [PHP] find all the numeric positions of a character that appears in a string multiple times > SPLIT to the rescue! Split tears up your string into lists (arrays) which > you can cycle through and extract normaly. So this is going to look like > hell but here we go... > > <? > $str = "<a href=http://foo.bar><b>test</b></a>"; > > $str_list1 = split ('<', $str); > // Note: $str_list1 now == array ("a href=http://foo.bar>", "b>test", "/b>", > "/a>") > $a = 0; > for ($i=0; $i<count($str_list1); $i++) > { > $str_list2 = split ('>', $str_list1[$i]); > for ($j=0; $j<count($str_list2); $j++) > { > if (empty($str_list2[$j])) > { > next; // skip blank indexes... > } > else > { > $str_list[$a] = $str_list2[$j]; // populate results list... > $a++; > } > } > } > > // print it out... > for ($i=0; $i<count($str_list); $i++) > { > echo "$str_list[$i]<br>"; > } > ?> > > > ... GAH! I second thought there's got to be a better way of doing this! > Anyway hope it helps some. > > -Kevin > > > > Hi guys. What is the best way to find all the numeric positions of a > > character that appears in a string multiple times? E.g. let's say I have > > the following string: > > > > $str = "<a href=http://foo.bar><b>test</b></a>"; > > > > And I want to parse the string and extract everything between <>. Now if > > the string only had a single occurrence of "<" and ">" I can use strpos() > > and substr() to extract it, but how do I do it for multiple occurrences? > I > > suppose I can keep chopping up the string to do this but I'm not sure if > > that's the best way. Thanks. > > > > David > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]