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

有帮助吗?

解决方案 2

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

其他提示

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
        )
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top