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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2 sep 2012 kl. 14.40 skrev Matijn Woudt:

> On Sun, Sep 2, 2012 at 6:23 AM, John Taylor-Johnston
> <jt.johnston@xxxxxxxxxxxxxx> 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


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux