Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shorten code to loop through array? 1

Status
Not open for further replies.

Pudsters

Technical User
Mar 16, 2006
151
US

I'm checking my form fields for links to other websites, if I find more than two I mark as SPAM. Is there a way to simplify this code? Maybe put all of my form fields in an array and loop through it and just write the preg_match code once? I just don't know how to do it!

PHP:
	if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['name'])) 
		$points += 4;	
	if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['business']))	
		$points += 1;		
	if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['address'])) 
		$points += 4;	
	if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['city']))	
		$points += 4;
	if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['state'])) 
		$points += 4;	
	if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['zip']))	
		$points += 4;
	if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['day_phone'])) 
		$points += 4;	
	if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['evening_phone']))	
	if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['comments']))	
		$points += 1;			
			
	// end score assignments
 
Hi

Pudsters said:
[gray](...)[/gray] if I find more than two [gray](...)[/gray]
Looks like having the same URL in two fields is a problem, but having a dozen URLs in one field is Ok. Is that really your intention ?

I would do it like this, counting for each URL the weight you assigned to the field where was found :
PHP:
[teal]<?php[/teal]

[navy]$weightMap[/navy] [teal]= [[/teal]
    [i][green]'name'[/green][/i] [teal]=>[/teal] [purple]4[/purple][teal],[/teal]
    [i][green]'business'[/green][/i] [teal]=>[/teal] [purple]1[/purple][teal],[/teal]
    [i][green]'address'[/green][/i] [teal]=>[/teal] [purple]4[/purple][teal],[/teal]
    [i][green]'city'[/green][/i] [teal]=>[/teal] [purple]4[/purple][teal],[/teal]
    [i][green]'state'[/green][/i] [teal]=>[/teal] [purple]4[/purple][teal],[/teal]
    [i][green]'zip'[/green][/i] [teal]=>[/teal] [purple]4[/purple][teal],[/teal]
    [i][green]'day_phone'[/green][/i] [teal]=>[/teal] [purple]4[/purple][teal],[/teal]
    [i][green]'evening_phone'[/green][/i] [teal]=>[/teal] [purple]4[/purple][teal],[/teal]
    [i][green]'comments'[/green][/i] [teal]=>[/teal] [purple]1[/purple][teal],[/teal]
[teal]];[/teal]

[navy]$points[/navy] [teal]=[/teal] [purple]0[/purple][teal];[/teal]
[b]foreach[/b] [teal]([/teal][navy]$weightMap[/navy] [b]as[/b] [navy]$field[/navy] [teal]=>[/teal] [navy]$weight[/navy][teal])[/teal]
    [b]if[/b] [teal]([/teal][b]isset[/b][teal]([/teal][navy]$_POST[/navy][teal][[/teal][navy]$field[/navy][teal]]))[/teal]
        [navy]$points[/navy] [teal]+=[/teal] [navy]$weight[/navy] [teal]*[/teal] [COLOR=orange]preg_match_all[/color][teal]([/teal][i][green]'/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i'[/green][/i][teal],[/teal] [navy]$_POST[/navy][teal][[/teal][navy]$field[/navy][teal]]);[/teal]

[COLOR=orange]var_dump[/color][teal]([/teal][navy]$points[/navy][teal]);[/teal]


Feherke.
feherke.github.io
 
No, not my intention. I have my form add up all scores from urls and from bad or spammy words like Sex, Viagra... And if the score reaches 4 points, the form won't submit and user gets a error message saying Marked as SPAM.

Anyway, this does look much cleaner. I see the reg expression is different than the one I used? Does yours not check for http:
Also can you explain the asterisk before the expression, I'm really trying to learn this.

Thanks and I will try this out.
 
Hi

Pudsters said:
Does yours not check for http:
It does. [tt]https?[/tt] means "http" followed by "s" repeated 0 or 1 times. So matches either "http" or "https".

Pudsters said:
Also can you explain the asterisk before the expression
No magic, just arithmetic multiplication. :)

I use [tt]preg_match_all()[/tt] which returns the number of matches found.

So if $_POST['business'] contains [ignore]" is similar to just like [/ignore], then [tt]preg_match_all()[/tt] returns 3, so $points gets incremented by the business field's weight of 1 multiplied with 3.


Feherke.
feherke.github.io
 
Awesome, pure genius. Thank you so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top