Jump to content

preg_match()


prezident

Recommended Posts

I've tried alot of different ways, google, but just can't find the correct way to do this.. I'm trying to search for a word in an inputted string.. here's the code

<html>
    <head><title>TESTING</title></head>
    
    <body>
        <form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>"/>
        email: <input type=text name=email value="" /><br/>
        feedback: <input type=text name=ex value=""/><br/>
        <input type="submit" value="submit" /><br/>
    </body>
</html>

<?php
$email = $_GET['email'];
$ex = $_GET['ex'];
$pattern = '/^[a-zA-Z0-9\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z]+$/';
//$pattern1 = '/nprezident/prezident/';

if(!preg_match($pattern, $email)){
    echo 'invalid email address<br/>';
} else { echo 'good to go<br/>'; }

if(preg_match('/nprezident/,/prezident/', $ex)){
    echo 'just something about that sentence is nice';
} else { echo 'no good'; }
?>

 

Ok the one I'm having a problem with is the second preg_match when trying to search for two words i can get one word to print but when i use add a second word it gives an error.

How do i add a second word? 

Link to comment
Share on other sites

Regular expressions are meant for complex string matching, this is basic:

 

if (strpos('nprezident', $ex) !== false || strpos('prezident', $ex) !== false) {

It's arguably a little longer to write, but there's no need to use regular expressions for something so simple.

Link to comment
Share on other sites

It's arguably a little longer to write, but there's no need to use regular expressions for something so simple.

 

I agree.

 

And rule of thumb is if you don't need regex you should try to avoid it.

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.