Jump to content

preg_match help


mattspriggs28

Recommended Posts

Hi, I have the following regular expression check to check for a valid URL:

 

preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $_prodLink)

 

This works great, except when a URL contains rounded brackets. Does anyone know how I can amend the above to allow the rounded brackets  ( ) in the URL?

Link to comment
https://forums.phpfreaks.com/topic/226956-preg_match-help/
Share on other sites

While those filters php provides are useful at times, I highly recommend you read up on regular expressions. Being able to customize your filter makes a world of difference.

preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $_prodLink)

 

Near as I can tell you need to have your delimiters set as forward slashes ( / ) not pipes ( | )

 

Change to this

preg_match('/^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$/i', $_prodLink)

Here is a good site to get started.

 

http://www.skdevelopment.com/php-regular-expressions.php

 

 

Here is a few that I use all the time.

 

 

function valPassword($input, $description, $strength = 1){

	switch($strength){
		case 1: //Low: 6 - 20 Characters Case insensitive.
			$pattern = "/^(?=.*[a-zA-Z0-9]).{6,20}$/";
			break;
		case 2: //Medium: 6 - 20 Characters: Case Insensitive : 1 Number Required.
			$pattern = "/^(?=.*\d)(?=.*[a-zA-Z]).{6,20}$/";
			break;
		case 3: //High: 8 - 20 Characters: Case Sensitive, 1 upper case, 1 lower case, 1 number Required
			$pattern = "/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/";
			break;
		case 4: //Max: Same as High, but requires 1 Special Character
			$pattern = "/^(?=.*\!\@\#\$\-\_)(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/";
			break;
	}
	$result = preg_match($pattern,$input);
	if($result){
		return true;
	}else{
		$this->errors[] = $description;
		return false;
	}
}

function valEmail($input, $description){ //Validates correct email syntax.
	$result = preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",$input);
	if($result){
		return true;
	}else{
		$this->errors[] = $description;
		return false;
	}
}

Link to comment
https://forums.phpfreaks.com/topic/226956-preg_match-help/#findComment-1170997
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.