Domanda

I want to be able to search for a specific code (up to three letters) that have been previously capitalized by strtoupper() to ensure correctness. I would like the search to look for the first character to be a "C" and then the last character(s) to be one of the following: L, M, P, C, BC, or RC. Therefore I am actually looking to search for CL, CM, CP, CC, CBC, or CRC.

I have the following:

// Make sure designation code is entered
   if (empty($designation)) {
     echo "<p class='error'>ERROR:  Designation code is required</p>";
     $valid = false; }
// Make sure designation code is not too long (3 characters)
   if (strlen($designation)>3) {
     echo "<p class='error'>ERROR:  Designation code cannot be more than 3 characters</p>";
  $valid = false; }
// Convert designation code to all uppercase
   $designation = strtoupper($designation);
// Make sure designation code is valid
   if (preg_match("/(CL|CM|CP|CC|CBC|CRC)/",$designation))  {
     echo "<p class='error'>ERROR:  Designation code not correct</p>";
     $valid = false; }

Everything works up to preg_match(); that's where nothing happens. I can type any letter and it will accept it as valid.

I also tried the following, but I just don't think this is correct:

if (preg_match("/^C[L|M|P|C|BC|CR]/",$designation)){
    echo "<p class='error'>ERROR:  Designation code not correct</p>";
    $valid = false; }
È stato utile?

Soluzione 4

Thank you everyone for your help. With all the input I was able to understand what was going on. As for my code the following works perfectly:

if (!preg_match('/C(L|M|P|C|BC|RC)/',$designation)) {
    echo "<p class='error'>ERROR:  Designation code not correct;
$valid = false;

Making preg_match() look for something other than what is in the list is what I was missing. This looks for the first letter of the code to be "C" and the rest to be one of the choices in the list. Additionally, I did not require i to ignore case because I already used strtoupper() to capitalize any input for this field. Now, if anything not in the list is entered the error message is displayed.

Altri suggerimenti

you can try

if (preg_match("/(C(L|M|P|C|BC|RC))/i",$designation)) {
     echo "<p class='error'>ERROR:  Designation code not correct</p>";
$valid = false; 
}

the fact is, if you're looking for an entire text, you will need preg_match_all since php doesnt support /g modifier

Two important things.

The regexp part:

 /^C(L|M|P)/i

means 'match anything starting with C followed by L or M or P independent of case', the [] denote a range, not a set of alternatives

The php part:

preg_match returns 1 on match. 0 on non-match and false on error. Your test should look like this if you want to be unambiguous

if(preg_match($pattern,$subject) === 1)
{
}

In your code example, the following should work

 if (preg_match('/^C(L|M|P|C|BC|RC)/i',$designation) === 1)  
 {
      echo "<p class='error'>ERROR:  Designation code not correct</p>";
      $valid = false; 
 }

As I understand your question, your logic is backwards. You want to accept it if the code is CL, etc., not reject it. In other words, you should reject it only if the preg_match() returns false. Just add a ! before the call to preg_match() to reverse your if condition, like so:

// Make sure designation code is valid
if (!preg_match("/(CL|CM|CP|CC|CBC|CRC)/",$designation))  { //notice the ! inside the parentheses
    echo "<p class='error'>ERROR:  Designation code not correct</p>";
    $valid = false;
}

If I have misunderstood your requirements, please let me know.

Also, as others have pointed out, you can use the i flag to avoid the call to strtoupper(). If you want to accept only the codes you listed above and not, say, FCL, add ^ and $ before and after the body of the regex, respectively. This also makes your strlen check unnecessary, unless you just want to show the error about the code length. This would make your entire code look like this:

// Make sure designation code is entered
if (empty($designation)) {
    echo "<p class='error'>ERROR:  Designation code is required</p>";
    $valid = false;
}
// Make sure designation code is not too long (3 characters)
// NOTE: This block is now optional
if (strlen($designation)>3) {
    echo "<p class='error'>ERROR:  Designation code cannot be more than 3 characters</p>";
    $valid = false;
}

// Make sure designation code is valid
if (!preg_match("/^(CL|CM|CP|CC|CBC|CRC)$/i",$designation))  { //notice the ! inside the parentheses
    echo "<p class='error'>ERROR:  Designation code not correct</p>";
    $valid = false;
}
/^((CL)|(CM)|(CP)|(CC)|(CBC)|(CRC))/i

Try http://www.regexr.com/ to play with RegExps. Option «i» means ignore case. No need to strtoupper.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top