2

Sorry about that but this is my first question on Stackoverflow.

I am trying to retrieve text from multiple .txt files and get their contents on a PHP file. The following code works properly:

<html>

<head>
  <title>
  </title>
</head>

<body>
  <?php  

foreach (glob("*.txt") as $filename) {
    echo "<br> $filename size " . filesize($filename) . "\n";
    $contents = htmlentities(file_get_contents( $filename ));


    echo $contents;

    }

?>
</body>

</html>

I would like to retrieve everything between the tags on my text files (let's call that step A) and then count the occurrences of every similar item between the span files

I tried solution(s) like the one:

I think that was a similar case: Link here

preg_match_all("/<span>(.+?)<\/span>/is", $str, $matches);

An example of my text file follows:

<span>Wolf</span>Hey guys how are rawr
<span>Harry</span><img src='smiles/heart.png'/> 
<span>Harry</span>My heart belongs to Conny
<span>Conny</span>Awww cute! 
<span>Xander</span>Grow up Conny…
<span>Zoro</span>I will chase you!

My output would like to be similar to:

$U ($U Being anything between the tags. In this case the name of a user eg. Harry), wrote $X ($X being the number of messages, from my example above for Harry being 2 messages) messages

Community
  • 1
  • 1
George G
  • 93
  • 7

2 Answers2

1

You can use some custom code like this :

preg_match_all("#<span>(.+)</span>#iU", $content, $matches);

$nbComments = array();
foreach($matches[1] as $match) {
    if(! isset($nbComments[$match])) {
        $nbComments[$match] = 0;
    }

    $nbComments[$match]++;
}

foreach($nbComments as $user => $nbComment) {
    echo sprintf("%s wrote %d messages<br />", $user, $nbComment);
}

See working code : https://eval.in/755234

Guillaume Sainthillier
  • 1,655
  • 1
  • 9
  • 13
  • $contents is my variable that stores the information retrieved from my .txt files. The output is: sdeHey guys how are u Harry ConnyAwww cute! XanderGrow up Conny… Zoro With your code added, unfortunately nothing changed in the output of the PHP file. – George G Mar 15 '17 at 15:03
  • Did you see the eval template ? It seems to be working – Guillaume Sainthillier Mar 15 '17 at 15:05
  • It does work on the eval template. However, the difference, as I mentioned before is that in my case as stated above a variable stores the String taken from a foreach loop. Maybe I made a mistake. I'll look it up and try to solve my problem – George G Mar 16 '17 at 08:27
  • The answer needed a small tweak of removing the "htmlentities" tag from my original code. After that, it worked like a charm. – George G Mar 20 '17 at 13:27
  • Nice ! Glad to help you ;-) – Guillaume Sainthillier Mar 20 '17 at 13:28
0
    <html>
<head>
<title>
</title>
</head>
<body><?php  

foreach (glob("*.txt") as $filename) {
    echo "<br> $filename size " . filesize($filename) . "\n";
    $contents = (file_get_contents( $filename ));

preg_match_all("#<span>(.+)</span>#iU", $contents, $matches);

$nbComments = array();
foreach($matches[1] as $match) {
    if(! isset($nbComments[$match])) {
        $nbComments[$match] = 0;
    }

    $nbComments[$match]++;
}

foreach($nbComments as $user => $nbComment) {
    echo printf("%s wrote %d messages<br />", $user, $nbComment);
}

    }


?>
</body>
</html>

It is now working as you said. Thank you very much fro the answer.

George G
  • 93
  • 7