Take list and print words that start with Florida only
Moderator: General Moderators
Take list and print words that start with Florida only
Hi,
I have a script that reads through a list of users. I need to print only users that start with the word Florida, or the letter "F".
Im not even sure what to call this type of feature. Is there an easy way to do this in PHP?
Thanks!
Fred
I have a script that reads through a list of users. I need to print only users that start with the word Florida, or the letter "F".
Im not even sure what to call this type of feature. Is there an easy way to do this in PHP?
Thanks!
Fred
- SantaGhost
- Forum Commoner
- Posts: 41
- Joined: Mon Sep 15, 2003 11:54 am
Code: Select all
get_user_list() {
// Returns an array of usernames.
$data = @file('../path/to/userlist.data');
if(is_array($data)) {
foreach($data as $user) {
$data = explode("|", $user);
$listї] = $dataї0];
}
return $list;
}
else return NULL;
}This was my first, simplest idea. I suspect this code may not be the fastest (processor-wise) option, but it should work.
edit: dammit, nevermind... wasn't missing a parentheses after all. double-oops.
Code: Select all
foreach($data AS $val) {
if (strtolower(substr($val, 0, 4)) == "flor")
echo $val;
}edit: dammit, nevermind... wasn't missing a parentheses after all. double-oops.
Last edited by Unipus on Mon Sep 15, 2003 12:25 pm, edited 2 times in total.
- SantaGhost
- Forum Commoner
- Posts: 41
- Joined: Mon Sep 15, 2003 11:54 am
You might want to take a look at the funciton array_search since you explode the users into an array:
http://nl2.php.net/manual/en/function.array-search.php
http://nl2.php.net/manual/en/function.array-search.php
Code: Select all
<?php
function get_user_list() {
$data = @file('users.txt');
$users = explode("|",$data[0]);
foreach($users as $user){
if (preg_match ("/florida/i", $user)) {
echo $user."<br />";
}
}
}
get_user_list();
?>