[FX.php List] PHP question

Dale Bengston dbengston at tds.net
Thu Jan 14 09:18:16 MST 2010


Hi Trish,

I love preg_match, but for this specific challenge, have you considered using explode()?

If you're always looking for Hall as the last word in a string, then:

	$wordArray = explode(' ', $_POST['Department_Name']);

... will split each word of your string into an array element, and then you just check the value of the last element. So, if your string is "Monty Hall", the resulting exploded array would be:

	0 => Monty
	1 => Hall

$lastElement = count($wordArray) -1;
$lastWord = $wordArray[$lastElement];

if $lastWord = 'Hall' then you have a match.

Hope this helps,
Dale

On Jan 14, 2010, at 9:43 AM, Leo R. Lundgren wrote:

> The strpos() solution doesn't make sure that the word occurs as the last thing in the string though. Just so you know. It would also match "anyHallCamel" just as just "Hall".
> 
> 
> 14 jan 2010 kl. 16.41 skrev VanBuskirk, Patricia:
> 
>> Guys, they BOTH worked ... I'm just brain dead this morning!  I realized
>> that the selections in the drop-down (other than "Student Request" are
>> upper case!  As soon as I changed the code to upper-case HALL, all works
>> perfectly!
>> 
>> Thanks again for your invaluable assistance!
>> 
>> Trish
>> 
>> 
>> -----Original Message-----
>> From: VanBuskirk, Patricia
>> Sent: Thursday, January 14, 2010 10:26 AM
>> To: 'FX.php Discussion List'
>> Subject: RE: [FX.php List] PHP question
>> 
>> Ok, I tried both:
>> 
>> if (isset($_POST['Department_Name']) && ($_POST['Department_Name'] ==
>> "Student Request" || strpos($_POST['Department_Name'],"Hall") !==
>> FALSE)) {
>> 		$Student_Dept_Request_Flag = "Student";
>> 		} else {
>> 		$Student_Dept_Request_Flag = "Department";
>> 		}
>> 
>> if (isset($_POST['Department_Name']) && ($_POST['Department_Name'] ==
>> "Student Request" || preg_match('/ Hall$/', $_POST['Department_Name'])
>> )) {
>> 		$Student_Dept_Request_Flag = "Student";
>> 		} else {
>> 		$Student_Dept_Request_Flag = "Department";
>> 		}
>> 
>> It doesn't error out, but I checked "Department" in the
>> Student_Dept_Request_Flag field and clicked on a ".. Hall" in the
>> Department dropdown and it does not change "Department" to "Student".
>> The other part of the equation does change it, however ... if I choose
>> "Student Request" from the dropdown.
>> 
>> 
>> 
>> -----Original Message-----
>> From: fx.php_list-bounces at mail.iviking.org
>> [mailto:fx.php_list-bounces at mail.iviking.org] On Behalf Of Jonathan
>> Schwartz
>> Sent: Thursday, January 14, 2010 9:54 AM
>> To: FX.php Discussion List
>> Subject: Re: [FX.php List] PHP question
>> 
>> How about this...
>> 
>> A strpos($haystack,$needle) would also work. The function is designed
>> to return the position of the "needle" in the "haystack", but also
>> returns "FALSE" if the needle does not exist.
>> 
>> I understand that it is faster than PREG.
>> 
>> if(strpos($_POST['Department_Name'],"Hall") !== FALSE)
>> {
>> Do this
>> }
>> 
>> To avoid problems resulting from upper/lower case, use stripos().
>> 
>> Hope there is no "Monte Hall Hall" ;-)
>> 
>> HTH
>> 
>> Jonathan
>> 
>>> A preg_match will work.
>>> 
>>> if (isset($_POST['Department_Name']) && preg_match('/hall$/i',
>>> $_POST['Department_Name']) {
>>> --
>>> GARETH EVANS
>>> 
>> 
>> 
>> -- 
>> Jonathan Schwartz
>> Exit 445 Group
>> jonathan at exit445.com
>> http://www.exit445.com
>> 415-370-5011
>> _______________________________________________
>> FX.php_List mailing list
>> FX.php_List at mail.iviking.org
>> http://www.iviking.org/mailman/listinfo/fx.php_list
>> _______________________________________________
>> FX.php_List mailing list
>> FX.php_List at mail.iviking.org
>> http://www.iviking.org/mailman/listinfo/fx.php_list
> 
> 
> 
> -|
> 
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list



More information about the FX.php_List mailing list