Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Shorten code to loop through array?

Shorten code to loop through array?

Shorten code to loop through array?


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!

CODE --> php

		$points += 4;	
		$points += 1;		
		$points += 4;	
		$points += 4;
		$points += 4;	
		$points += 4;
		$points += 4;	
		$points += 1;			
	// end score assignments 

RE: Shorten code to loop through array?


Quote (Pudsters)

(...) if I find more than two (...)
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 :



$weightMap = [
    'name' => 4,
    'business' => 1,
    'address' => 4,
    'city' => 4,
    'state' => 4,
    'zip' => 4,
    'day_phone' => 4,
    'evening_phone' => 4,
    'comments' => 1,

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



RE: Shorten code to loop through array?

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.

RE: Shorten code to loop through array?


Quote (Pudsters)

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

Quote (Pudsters)

Also can you explain the asterisk before the expression
No magic, just arithmetic multiplication. smile

I use preg_match_all() which returns the number of matches found.

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


RE: Shorten code to loop through array?

Awesome, pure genius. Thank you so much.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close