Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

PHP explode

Name: Anonymous 2010-05-03 2:24

Trying hard to explode a flat file hit counter.

Here is an example:

home:1|news:33|contact:4

I need to explode first by the | to get the page id and the hits, then I need to explode again by the : to separate them.

I tried using foreach but that was a mess. Any ideas?

Name: Anonymous 2010-05-03 2:33

C-4

Name: Anonymous 2010-05-03 8:11

Name: Anonymous 2010-05-03 10:48

>>1
explode
Why don't they just call it split?

Name: Anonymous 2010-05-03 11:01

>>4
Then they couldn't call the reverse function implode.

Name: Anonymous 2010-05-03 11:22

>>5
They could have called it join. But we must forgive them, they came from a language which thought it would be fun to have things like ``chomp'' and ``slurp mode.''

Name: Anonymous 2010-05-03 15:14

>>6
I don't understand what you're trying to say.

Name: Anonymous 2010-05-03 18:09

>>7
Respectable programmers would never use fun words such as IsComputerOnFire or cudder in their works.
Good words to use in a program are DbReportFactory and bayesian_average.

Name: Anonymous 2010-05-03 18:12

EXPLODE MY ANUS

Name: Anonymous 2010-05-03 18:21

>>8
intercalate

Name: Anonymous 2010-05-04 3:31

This is what you want:


"home:1|news:33|contact:4".split("|").map {|x| a,b = x.split(":"); {a => b.to_i} }.inject {|a,b| a.merge! b  }

Name: Anonymous 2010-05-04 4:46

/(\d+)/g

Name: Anonymous 2010-05-04 12:03

<?php
// trollprog.php
function parseHitsFile($filename)
{
    $file = file_get_contents($filename);
    $pages = explode('|', $file);
    $hits = array();
    foreach ($pages as $page) {
        $tmp = explode(':', $page);
        $hits[ $tmp[0] ] = $tmp[1];
    }
    return $hits;
}


$hitstats = parseHitsFile('hits.txt');
arsort($hitstats);
foreach ($hitstats as $page => $hits) {
    echo "$hits &mdash; $page";
}
?>

Name: Anonymous 2010-05-04 12:12

>>13,11,1
%ctr=split/[:|]/

Don't change these.
Name: Email:
Entire Thread Thread List