Re: array_push

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

 



2 sep 2012 kl. 19.48 skrev John Taylor-Johnston:

> How can I clean this up?
>>> 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.
>> 2) How could I suck it into one nice easy to handle array?
>> 
>> |$mynewarray=|array {
>>  [0]=> "Residential Fire Determined to be Accidental in Nature ..."
>>  [1]=> "Arrest Made in Residential Fire ..."
>> } 
> I was hoping preg_match_all would return strings.  I w/as hoping |$matches[1] was a string.|/
> 
> source: http://www.cegepsherbrooke.qc.ca/~languesmodernes/test/test4.phps
> result: http://www.cegepsherbrooke.qc.ca/~languesmodernes/test/test4.php

Have you read up on 'preg_match_all' in the manual? What makes you think that preg_match_all returns strings? "$matches[1]", in the above case contains an array with all matches from the parenthesized subpattern, which is "(.+).

> This is ugly. How can I clean this up like this?
> 
> $mynewarray= array {
>  [0]=> "Residential Fire Determined to be Accidental in Nature ..."
>  [1]=> "Arrest Made in Residential Fire ..."
> }

Why not add two lines of code within the first loop?

$chunks = explode("-30-", $mystring);
foreach($chunks as $chunk) {
    preg_match_all("/News Releases\n(.+)/s", $chunk, $matches);
    foreach($matches[1] as $matched_text_line) {
    	$mynewarray[] = $matched_text_line;
    }
}

Besides the regex, this is pretty basic php. 

/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