Jump to content

Can anybody help with a preg match error?


wright67uk

Recommended Posts

Can anybody help me with the preg match in my code;

 

elseif  ($Website == '' or 
(preg_match ("[a-zA-Z0-9\-\.]+\.(com|COM|Com|ORG|Org|org|net|NET|Net|edu|EDU|Edu|org|ORG|Org|NET|net|Net|co.uk|CO.UK|Co.uk|Co.Uk|co.Uk|CO.uk|co.UK", $Website)))

 

I have the error; 

 

Warning: preg_match() [function.preg-match]: Unknown modifier '+'

 

 

<?php
//code selected

if(isset($_POST['submit']))
{
$drop = mysql_real_escape_string($_POST['drop_1']);
$tier_two = mysql_real_escape_string($_POST['Subtype']);
$Name = mysql_real_escape_string($_POST["Name"]);
$Phone = mysql_real_escape_string($_POST["Phone"]);
$Email = mysql_real_escape_string($_POST["Email"]);
$Postcode = mysql_real_escape_string($_POST["Postcode"]);
$Website = mysql_real_escape_string($_POST["Website"]);
if($Name == '')
{
	die('<br> You did not complete the name field, please try again');
}
elseif  ($Phone == '' or (preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $Phone)))
{	//LINE74
	die('<br> You completed the telephone field incorrectly, please try again');
}		
elseif  ($Email == '' or (!filter_var($Email, FILTER_VALIDATE_EMAIL)))
{
	die('<br> You completed the Email field incorrectly, please try again');
}									   
elseif  ($Postcode == '' or (!checkPostcode($Postcode)))
{
	die('<br> You did not complete the Postcode field correctly, please try again');
}		  
elseif  ($Website == '' or 
(preg_match ("[a-zA-Z0-9\-\.]+\.(com|COM|Com|ORG|Org|org|net|NET|Net|edu|EDU|Edu|org|ORG|Org|NET|net|Net|co.uk|CO.UK|Co.uk|Co.Uk|co.Uk|CO.uk|co.UK", $Website)))
{
	die('<br>You completed the website field incorrectly, please try again');
}
else
{
	echo("Thankyou for submiting your details, you will be added to our directory shortly");
}}

Link to comment
Share on other sites

preg_match expects the pattern argument to be formatted as such: "[delimiter][pattern][delimiter][modifier]" The delimiter separates the pattern from any modifiers you might have.  It can be pretty much any non-alphanumeric character you want, as long as you remember to escape it if you use it as part of your actual pattern.  So for instance, you need to do

 

"~pattern~modifier"

 

or

 

"/pattern/modifier"

 

But you don't actually have any modifiers in there so You just need to do like

 

"~pattern~"

 

So the reason you got that specific error is that you can use [ and ] as the pattern delimiter, so it's seeing your [ at the start of your pattern, then seeing the ] later on and thinking it's the end of your pattern and then thinks what comes next is are modifiers and since the next thing is a + and that's not a valid modifier, it throws an error.

Link to comment
Share on other sites

Yep thats fine, I can add to the domain part.

 

The only thing is that I get an error;

 

Parse error: syntax error, unexpected ')'  on line 112.  I dont have a ')' on line 112.

Ive commented on line 112.  Im very new to regex / pregmatch etc.

 

<?php 
function checkPostcode (&$toCheck) 
{ 
$alpha1 = "[abcdefghijklmnoprstuwyz]";                           
$alpha2 = "[abcdefghklmnopqrstuvwxy]";                          
$alpha3 = "[abcdefghjkstuw]";                                   
$alpha4 = "[abehmnprvwxy]";                                       
$alpha5 = "[abdefghjlnpqrstuwxyz]";                               
$pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';  
$pcexp[1] =  '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';
$pcexp[2] =  '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';
$pcexp[3] =  '^(gir)(0aa)$';  
$pcexp[4] = '^(bfpo)([0-9]{1,4})$';
$pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$'; 
$Postcode = strtolower($toCheck);
$Postcode = str_replace (' ', '', $Postcode);
$valid = false;
foreach ($pcexp as $regexp) {    
if (ereg($regexp,$Postcode, $matches)) {      
$toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);    
$toCheck = ereg_replace ('C\/O', 'c/o ', $toCheck);
$valid = true;
break;     
}  
}  
if ($valid){return true;} else {return false;};
}
if(isset($_POST['submit']))
{
$drop = mysql_real_escape_string($_POST['drop_1']);
$tier_two = mysql_real_escape_string($_POST['Subtype']);
$Name = mysql_real_escape_string($_POST["Name"]);
$Phone = mysql_real_escape_string($_POST["Phone"]);
$Email = mysql_real_escape_string($_POST["Email"]);
$Postcode = mysql_real_escape_string($_POST["Postcode"]);
$Website = mysql_real_escape_string($_POST["Website"]);
if($Name == '')
{
	die('<br> You did not complete the name field, please try again');
}
elseif  ($Phone == '' or (preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $Phone)))
{	//LINE74
	die('<br> You completed the telephone field incorrectly, please try again');
}		
elseif  ($Email == '' or (!filter_var($Email, FILTER_VALIDATE_EMAIL)))
{
	die('<br> You completed the Email field incorrectly, please try again');
}									   
elseif  ($Postcode == '' or (!checkPostcode($Postcode)))
{
	die('<br> You did not complete the Postcode field correctly, please try again');
}		  
elseif  ($Website == '' or 
	(preg_match ("~a-zA-Z0-9-\.+\.com|COM|Com|ORG|Org|org|net|NET|Net|edu|EDU|Edu|org|ORG|Org|NET|net|Net|co.uk|CO.UK|Co.uk|Co.Uk|co.Uk|CO.uk|co.UK~", $Website)))
{  //THIS IS LINE 112 																									THIS IS LINE 112
	die('<br>You completed the website field incorrectly, please try again');
}
else
{
	echo("Thankyou for submiting your details, you will be added to our directory shortly");
}}
$query = ("INSERT INTO business (`id`, `Name`,  `Type`, `Subtype`, `Phone`, `Email`, `Postcode`, `WebAddress`)
		  VALUES ('NULL', '$Name', '$drop', '$tier_two' , '$Phone', '$Email', '$Postcode', '$Website')");
mysql_query($query) or die ( "<br>Query: $query<br>Error: " .mysql_error());
?>

Link to comment
Share on other sites

I'm starting to get really fed up with GODADDY and the fact that my PHP files take sometimes 10 minutes to update on my server.

Does anybody else have similiar problems with this or other hosting companies?

 

Also, what board should I post this on if Id like to start a new thread?

Link to comment
Share on other sites

I see your point, but im not overly worried.

 

Im making a directory and just want each listing to have a similiar format.

If the user doesnt type in their own email address, they dont get enquiries.

 

My email field is using FILTER_VALIDATE_EMAIL

but its the website address that im working on at the moment.

 

Im not sure if php has inbuilt filters for web addresses/url's???

Link to comment
Share on other sites

I got this to work well in my script in the end.  Just thought i'd share it.

I had a comment about COM com cOm and didnt realise that if i add 'i' at the end of my closing bracket that it would allow case to be ignored.

 

elseif  ($Website == '' or (!preg_match("~^[a-z0-9.-]+\.(com|org|net|edu|co.uk)~i", $Website)))

die('<br>You completed the website field incorrectly, please try again');

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.