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
https://forums.phpfreaks.com/topic/224189-preg_match/
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
https://forums.phpfreaks.com/topic/224189-preg_match/#findComment-1158391
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.