Re: Re: String parsing and Array sorting

From: Date: Tue, 06 Aug 2002 14:14:19 +0000
Subject: Re: Re: String parsing and Array sorting
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-111375@lists.php.net to get a copy of this message
What I wound up doing is making a copy ($result) of the array $biofields, sorting the copy and then looping through the original $biofields array and then using array_search to find $biofields[$i] in $result. Based on the array key returned I can now grab the position of the current keyword plus the length by grabbing the position of the next keyword minus the position of the current one. If it is the last keyword I calculate the length by grabbing the length of the entire string minus the position of the last keyword. Thanks for the tip Richard. I will try your method as well. ere is the code I came up with: $bio = "Kenneth J. Abdo: A former professional musician and disc jockey, Mr. Abdo is an attorney, a principal shareholder and vice president of Abdo &#38; Abdo, P.A. He concentrates in business, entertainment, intellectual property, employment and contract law, and negotiates and counsels on a wide range of entertainment contracts and matters. Mr. Abdo is an adjunct professor of art, entertainment and sports law at William Mitchell and Hamline University Law Schools, where he lectures on music, film, media, sports, trademarks and copyrights. He also lectures and writes for various legal and public audiences. His writing includes two chapters in The Practical Musician, 1993."; $bio .= "Education: JD 1982, William Mitchell (staff writer, William Mitchell Law Review; Moot Court Society); BA 1979, University of Minnesota (Phi Beta Kappa; Omicron Delta Kappa)."; $bio .= "Admitted: 1983 Minnesota; 1983 U.S. Dist. Ct. (MN)."; $bio .= "Representative Clients: Mr. Abdo?s clients include businesses and individuals in the entertainment business, including radio and television talents and stations, actors, for-profit and nonprofit theaters, music recording studios, independent film and music projects, comedians, agents, managers, writers, musicians, film and theater production companies, special event production companies, literary publishers, sports figures, entertainer licensers and merchandisers."; $bio .= "Professional Associations: ABA [Forum on Entertainment and Sports Industry annual conference (Planning Committee)]; MSBA [Arts and Entertainment Law Section (Chair 1991-93)]; Music Law Committee (Chair 1993-present); Seminar on Entertainment and Sports Law (Cochair 1992-present)."; $bio .= "Employment History: Law Clerk/Attorney 1979-present, Abdo &#38; Abdo, P.A."; $bio .= "Community Involvement: The Cricket Theatre [Board of Directors (Chair)]; Concerts for the Environment; Midwest Center for Arts, Entertainment and Law (Board of Directors); Minneapolis Athletic Club (Board of Directors); Youth Forum Minnesota (Board of Directors)."; $bio .= "Firm: Since 1936, Abdo &#38; Abdo, P.A., has provided businesses and individuals with personalized legal services. The firm?s six attorneys provide legal counsel focused on small and medium-sized businesses? specific needs, including start-ups, maintenance, mergers, acquisitions, partnerships, securities, real estate, employment, intellectual property (copyrights and trademarks), entertainment, debtor, creditor, and general and complex litigation. The firm also serves the legal needs of individuals, professionals and business owners in employment law, contracts, wills, probate and estate planning."; echo "Original unformatted: \n" . $bio . "\n"; $keywords[0]="Education:"; $keywords[1]="Admitted:"; $keywords[2]="Employment History:"; $keywords[3]="Professional Associations:"; $keywords[4]="Representative Clients:"; $keywords[5]="Community Involvement:"; $keywords[6]="Firm:"; // Loop through each keyword and add it (if found) to the biofields array in position,keyword forrmat $i=0; $j=-1; for ($i=0;$i<=count($keywords);$i++) { $keypos = strpos($bio, $keywords[$i]); if (!$keypos === false ) { $j++; $biofields[$j] = "$keypos, $keywords[$j]"; } } print_r($biofields); // make a copy of biofields array $result = $biofields; //sort the result array sort($result, SORT_NUMERIC); print_r($result); $newbio = "Formatted bio: \n"; $newbio .= substr($bio,0,substr($result[0],0,strpos($result[0],","))) . "\n"; $i=0; // loop through biofields for ($i=0;$i<=count($biofields)-1;++$i) { // search for $biofield[$x] in $result $test = array_search($biofields[$i], $result); //get start position of "field" $startPos = substr($result[$test],0,strpos($result[$test],",")); //get number of characters to extract // check if the $startpos if the postion of the last "field" //echo "array index: " . $test . " count of indices: " . count($result) . "\n"; if ($test == count($result)-1) { $numChars = strlen($bio) - $startPos; } else { $numChars = substr($result[$test+1],0,strpos($result[$test+1],",")) - $startPos; } $newbio .= substr($bio, $startPos, $numChars) . "\n"; } echo $newbio; ----- Original Message ----- From: "Richard Lynch" <rich@phpbootcamp.com> To: "Geoff Hankerson" <ghank@lawlead.com> Cc: <php-general@lists.php.net> Sent: Monday, August 05, 2002 6:24 PM Subject: [PHP] Re: String parsing and Array sorting > >I am having trouble conceptualizing a solution to a fairly complicated (for > >me) string parsing exercise. > > > >The problem is I need to work with "fields" in a text string which is > >essentially a profile of a professional person (doctor, lawyer , etc...) > > > >So say the profile is this: > >John Doe the best in the world. > >Education: School of hard Knocks 2000 > >Admitted: 2001. > >Employment History: John Doe Inc, 2000- present > >Professional Associations: PTA > >Representative Clients: My dog > > > >Obviously this is a simplified, ficticious example. This profile will come > >from a database but here I'l just stuff it in a string variable for > >simplicity. > >$bio = "Me \n Education: School of hard Knocks 2000. \n Admitted: 2001. \n > >Employment History: none \n Professional Associations: PTA \n Representative > >Clients: My dog"; > > > >The "delimiters" of the text field can be put into an array as follows: > > $keywords[0]="Education:"; > > $keywords[1]="Admitted:"; > > $keywords[2]="Employment History:"; > > $keywords[3]="Professional Associations:"; > > $keywords[4]="Representative Clients:"; > > $keywords[5]="Community Involvement:"; > > $keywords[6]="Firm:"; > > > >Now I can loop through the text field ($bio) looking for each keyword and > >its position and add it to another array > >as follows: > >$i=0; > > $j=-1; > > for ($i=0;$i<=count($keywords);$i++) { > > $keypos = strpos($bio, $keywords[$i]); > > > > if (!$keypos === false ) > > { > > $j++; > > $biofields[$j][0] = $keypos; > > $biofields[$j][1] = $keywords[$j]; > > } > > } > > > >I can now output this and get this (in position keyword format): > > > > 3 Education: > >45 Admitted: > >66 Employment History: > >93 Professional Associations: > >126 Representative Clients: > > > >With this information I can parse through the "bio" and get each > >"field" if > >they are always in the same, static order, but in reality this is not the > >case. So I need to sort the array somehow or use some other method to make > >this flexible enough to account for any order of "keyword delimiters". > > <?php > asort($biofields); # http://php.net/asort Now you know > which keys come > first. > reset($biofields); > $laststart = 0; > $lastfield = ''; > while (list($offset, $field) = each($biofields)){ > # Get the chunk starting at the current field: > $values[$field] = substr($bio, $laststart + strlen($lastfield)); > # In our *last* field, chop off all the stuff from our current field > forward: > if ($lastfield){ > $values[$lastfield] = substr($values[$lastfield], 0, $laststart + > $offset - 1); > } > # Play 'leapfrog' for the next field: > $laststart = $offset; > $lastfield = $field; > } > ?> > > -- > Like Music? http://l-i-e.com/artists.htm > I'm looking for a PRO QUALITY two-input sound card supported by Linux (any > major distro). Need to record live events (mixed already) to stereo > CD-quality. Soundcard Recommendations? > Software to handle the recording? Don't need fancy mixer stuff. Zero (0) > post-production time. Just raw PCM/WAV/AIFF 16+ bit, 44.1KHz, Stereo > audio-to-disk. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#111375) next »