Re: array_push

From: Date: Sun, 02 Sep 2012 18:38:02 +0000
Subject: Re: array_push
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.general 
Request: Send a blank email to php-general+get-318947@lists.php.net to get a copy of this message
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©nR"v > Ú!Î¥á$Ç > 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

« previous php.general (#318947) next »