Take list and print words that start with Florida only

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Frederick
Forum Newbie
Posts: 13
Joined: Fri Nov 15, 2002 9:42 am

Take list and print words that start with Florida only

Post 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
User avatar
SantaGhost
Forum Commoner
Posts: 41
Joined: Mon Sep 15, 2003 11:54 am

Post by SantaGhost »

maybe you could post your script which lists all the users
Frederick
Forum Newbie
Posts: 13
Joined: Fri Nov 15, 2002 9:42 am

Post 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;
}
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post 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.
Last edited by Unipus on Mon Sep 15, 2003 12:25 pm, edited 2 times in total.
User avatar
SantaGhost
Forum Commoner
Posts: 41
Joined: Mon Sep 15, 2003 11:54 am

Post 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
Frederick
Forum Newbie
Posts: 13
Joined: Fri Nov 15, 2002 9:42 am

Post by Frederick »

Ill give it a shot, thank you.
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post 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?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

alter your text how? I'm confused. I do know that regexp are significantly slower than other methods.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
Post Reply