Re: find all the numeric positions of a character that appears in a string multiple times

From: Date: Thu, 03 Jan 2002 20:30:17 +0000
Subject: Re: find all the numeric positions of a character that appears in a string multiple times
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-79360@lists.php.net to get a copy of this message
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: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#79360) next »