0

Is it possible to just use this code alone to do two different regex matches?

One reg-ex match for X digits and the other for a given string "CAT" only, or do I need to create two different forms, one for each: X digits and one for the string "CAT"?

Furthermore is there a better php method for this or I am headed in the right direction?

Thank you in advance.

<form action ="callself.php" method = "post">
 Enter a numeric value:
<br/>
<input type = "text" name = "number"/>
<br /><br />
<input type = "submit"/>
<br /><br />
</form>

I am a newb(still in the process of learning, thank you in advance for not flaming)

2
  • I'm a bit confused, so you're trying to match either "CAT" or any number of digits from $_POST['number']? What do you want returned if a) x number of digits are matched c) CAT is matched? Commented Nov 19, 2009 at 11:29
  • Sorry I did not clarify, I just split the form into two chunks one for the digits to be matched via regex that uses the variable number and the other form uses the variable string to regex match CAT, sorry about that. I only posted one aspect of the code. Commented Nov 19, 2009 at 11:54

1 Answer 1

2

If you just want to check if the field contanins either CAT or a sequence of digit, you can use this check:

if (preg_match('/^(?:CAT|\d+)$/', $field))

if you need to act differently in the two cases, explain better.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess, using the above method, I might as well just remove the second form I had created, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.