Domanda

Ok, I am working on a flatfile shoutbox, and I am trying to achieve a way to get the username from the flatfile and making it a variable so I can use it to make a call to the database to check if the user is admin so they can delete/ban users directly from the shoutbox.

This is an example line in the flatfile

<div><i><div class='date'>12/08/2012 18:56 pm&nbsp;&nbsp;</div></i> <div class='groupAdmin'><b>Admin</b></div><b><a href='javascript:;' onclick="shout_insert('@kira423- ')">kira423</a>:</b> hiya :D</div>

So I wanna take the username which is kira423 in this case and create a variable such as $shoutname and make it equal kira423

I have tried a google search and looked around on here, but was unable to find an answer, so I am hoping that I can get some insight on how to do this with a question of my own here.

Thanks,

Kira

È stato utile?

Soluzione

You should use preg_match for those tasks like this:

preg_match_all('|<div class=\'date\'>(?P<date>.*?)&nbsp;.*<a.*>(?P<user>.*)</a>|i', $data, $matches);
var_dump($matches);

Interating through all array elements:

foreach ($matches['user'] as $key => $user) {
    var_dump($user);
}

Altri suggerimenti

I think you should just parse each line in the flatfile as HTML (there are simple HTML tags used), just like described in PHP Parse HTML code (or type "php parse HTML" in google). Then you may access the username (kira123) from an array or whatever.

PS HTML is not the best way you can store messages to display. Even CSV seems to be better - it'd be "kira123;date;some text" - it's easier to read and to access each part. When displaying, use the standar decorator pattern.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top