0

i have a file structured like this: http://pastebin.com/dvja3YkT and my goal is to get the text after group:, since i have the name under Users: I tried exploding like this:

$boom = explode($open, "$user");

Where $open is a file_get_contents that works well, and $user is an username. When i use $boom[0], it outputs the user i am looking for, but when i try $boom[1] it says Notice: Undefined offset: 1. Is there any way to get the group name after

group:

if i know the username under

Users:
   Username:

?

PS: i forgot to mention that the file will keep updating, so i need a method to get the user's group if he's in the list, otherwise i send him an error message

Joe96
  • 17
  • 1
  • 1
  • 6
  • possible duplicate of [PHP YAML Parsers](http://stackoverflow.com/questions/294355/php-yaml-parsers) – pdu Mar 30 '14 at 20:26

2 Answers2

1

You can use the following regex

$input_lines is the strings in the file

preg_match_all("/group:(.*)$/m", $input_lines, $output_array);

$output_array = 

Array
(
    [0] => Array
        (
            [0] => group: Builder
            [1] => group: Owner
            [2] => group: Owner
            [3] => group: Moderator
            [4] => group: VIP
            [5] => group: Moderator
            [6] => group: Admin
            [7] => group: Builder
            [8] => group: Co-Owner
            [9] => group: VIP
        )

    [1] => Array
        (
            [0] =>  Builder
            [1] =>  Owner
            [2] =>  Owner
            [3] =>  Moderator
            [4] =>  VIP
            [5] =>  Moderator
            [6] =>  Admin
            [7] =>  Builder
            [8] =>  Co-Owner
            [9] =>  VIP
        )
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
0

You've passed the arguments to explode in the wrong order, it should be

$boom = explode("$user", $open);

anyway here is a simple piece of code get the group

$boom = explode("$user:", $open);
$boom = explode("group: ", $boom[1]);
$boom = explode("\n", $boom[1]);
echo $boom[0];

http://ideone.com/DPvrVj

Musa
  • 96,336
  • 17
  • 118
  • 137
  • Thank you, it worked! Do you know how to send an error message if user is not in the list? if(!$boom) { echo "message"; } will return a message, but also a notice – Joe96 Apr 17 '14 at 19:41
  • @user3025008 Try `if (count($boom) == 1){echo "message"; }` – Musa Apr 17 '14 at 19:59