Re: extract Occurrences AFTER ... and before "-30-"

From: Date: Sun, 02 Sep 2012 14:33:00 +0000
Subject: Re: extract Occurrences AFTER ... and before "-30-"
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-318941@lists.php.net to get a copy of this message
2 sep 2012 kl. 14.40 skrev Matijn Woudt: > On Sun, Sep 2, 2012 at 6:23 AM, John Taylor-Johnston > <jt.johnston@usherbrooke.ca> wrote: >> See: >> >> http://www.cegepsherbrooke.qc.ca/~languesmodernes/test/test.php >> >> http://www.cegepsherbrooke.qc.ca/~languesmodernes/test/test.phps >> >> In $mystring, I need to extract everything between "|News Releases|" and >> "-30". >> >> The thing now is $mystring might contain many instances of "|News Releases|" >> and "-30". >> >> How do I deal with this? My code only catches the first instance. >> >> Thanks for you help so far. >> >> John >> > > You could use substr to retrieve the rest of the string and just start > over (do it in a while loop to catch all). > Though, it's probably not really efficient if you have long strings. > You'd be better off with preg_match. You can do it all with a single > line of code, albeit that regex takes quite some time to figure out if > not experienced. > > - Matijn > > PS. Please don't top post on this and probably any mailing list. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > My approach would be to split the hole text into smaller chunks (with e.g. explode()) and extract the interesting parts with a regular expression. Maybe this will give you some ideas: $chunks = explode("-30-", $mystring); foreach($chunks as $chunk) { preg_match_all("/News Releases\n(.+)/s", $chunk, $matches); var_dump($matches[1]); } The regex matches all text between "News Releases" and the end of the chunk. /frank

« previous php.general (#318941) next »