Page 1 of 1
Take list and print words that start with Florida only
Posted: Mon Sep 15, 2003 11:30 am
by Frederick
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
Posted: Mon Sep 15, 2003 11:57 am
by SantaGhost
maybe you could post your script which lists all the users
Posted: Mon Sep 15, 2003 12:02 pm
by Frederick
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;
}
Posted: Mon Sep 15, 2003 12:12 pm
by Unipus
This was my first, simplest idea. I suspect this code may not be the fastest (processor-wise) option, but it should work.
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.
Posted: Mon Sep 15, 2003 12:17 pm
by SantaGhost
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
Posted: Mon Sep 15, 2003 12:19 pm
by Frederick
Ill give it a shot, thank you.
Posted: Mon Sep 15, 2003 12:33 pm
by Drachlen
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();
?>
Try this?
Posted: Mon Sep 15, 2003 3:20 pm
by m3rajk
drachlen already did what i was going to say. i suggest that before any strto or substr checks since it's less likely to alter your text
Posted: Mon Sep 15, 2003 3:36 pm
by Unipus
alter your text how? I'm confused. I do know that regexp are significantly slower than other methods.
Posted: Mon Sep 15, 2003 4:06 pm
by m3rajk
actually the prce (how you use perl) is faster than the posix.
but he had a chekc to see if a substring was equal to something. doing that wrong can alter your string. you end up with the substring instead