hipromark Posted July 5, 2012 Share Posted July 5, 2012 Hello, It´s Me again. I am using regular expressions (Chapter 10, script 10.7) but the script doesn´t work, it seems it don´t recognizes them. I looked at PHP manual and it says eregi and ereg are obsolete. Is it true? what can I use instead? Link to comment Share on other sites More sharing options...
HartleySan Posted July 5, 2012 Share Posted July 5, 2012 You should use the PREG (Perl Regular Expression) library, which should be built into all PHP installations. Specifically, you should use the preg_match function. For global searches, use the preg_match_all function. The following is an example of a simple PREG regex match: $pattern = '/d[o|ra]g/'; $target_str = 'The dog dragged the stick across the yard.'; preg_match_all($pattern, $target_str, $matches); At this point, the $matches variable would be an array containing various bits of info about the two matches in the string ('dog' and 'drag'). See php.net for details. Link to comment Share on other sites More sharing options...
hipromark Posted July 7, 2012 Author Share Posted July 7, 2012 Thanks for your help, but how can I use preg_match_all in the same way as eregi and ereg are used in script 10.7. I need to do somethimng like: if (!eregi ('^[[:alpha:]\.\' \-]{4,}$', stripslashes(trim($_POST['name'])))) { $problem = TRUE; $message .= '<p>Please enter a valid name.</p>'; If I use preg_match_all I have to specify $matches, i don´t know how to handle $matches! Link to comment Share on other sites More sharing options...
margaux Posted July 7, 2012 Share Posted July 7, 2012 I don't have this book so I'm not sure the context in which you are using preg_match. I'm guessing that you are using it to validate some form fields. You can use preg_match without the 3rd optional parameter. if (!preg_match($pattern, $string)) { $problem=TRUE; $message .='<p>Please enter a valid name.</p>'; } 1 Link to comment Share on other sites More sharing options...
hipromark Posted July 8, 2012 Author Share Posted July 8, 2012 Ok. Now I did this: $pattern = '^[[:alpha:]\.\' \-]{4,}$'; $string = $_POST['nombre']; if(!preg_match($pattern, $string)) { echo '<p>please enter a valid name</p>'; } else { echo 'Your name is ok'; } ?> And I get this error message: Warning: preg_match() [function.preg-match]: No ending delimiter '^' Link to comment Share on other sites More sharing options...
Larry Posted July 8, 2012 Share Posted July 8, 2012 That's because you didn't use a starting or ending delimiter. PCRE uses a different syntax that POSIX. Link to comment Share on other sites More sharing options...
hipromark Posted July 10, 2012 Author Share Posted July 10, 2012 Ok I looked at PHP manual and now I see the problem was on the delimiter, I used # and it work perfectly. But regular expressions themselves are the same in POSIX and in PCRE, at least most of them am I right? Link to comment Share on other sites More sharing options...
Larry Posted July 11, 2012 Share Posted July 11, 2012 I don't want to say "Yes" to the idea that the regular expressions are largely the same in POSIX and PCRE, because I don't want anyone thinking you can mindlessly swap the two. Much of the syntax overlaps, but you should learn PCRE on its own to make sure you're using PCRE correctly. Link to comment Share on other sites More sharing options...
hipromark Posted August 11, 2012 Author Share Posted August 11, 2012 Hello, I am still having trouble with regular expressions. I made this script: <?php if (isset($_POST['submit'])) { $pattern = '#^[01[:alpha:]%]$#'; $string = $_POST['name']; if(!preg_match($pattern, $string)) { echo 'please use valid characters'; } else { echo 'Name format is correct'; } } Ok so when I run this script, even if I use alphabetic caracters, 1, 2, or % the message I receive is "please use valid characters", so the pattern is not recognized by PHP What am I doing wrong? Can anybody tell me? Link to comment Share on other sites More sharing options...
Larry Posted August 11, 2012 Share Posted August 11, 2012 The pattern you've created doesn't make sense to me. What is it that you exactly want to allow or not allow? Link to comment Share on other sites More sharing options...
hipromark Posted August 13, 2012 Author Share Posted August 13, 2012 This is an example that I saw in php.net, there they say this pattern finds: 0, 1, alfabetic characters, or %, but it doesn't do that in my script, is it because I have to install something on PHP or what is happening? Here is the page from where I take the example (Is in spanish but may be it has a mirror page in english): http://tr2.php.net/manual/es/regexp.reference.character-classes.php Link to comment Share on other sites More sharing options...
Larry Posted August 13, 2012 Share Posted August 13, 2012 No, you don't have to install anything. Link to comment Share on other sites More sharing options...
hipromark Posted August 27, 2012 Author Share Posted August 27, 2012 Ok, let me try this way. Could you give me a pattern with preg match that match alphabetic characters, the numbers 1,2 and 3, and '%' ? I just need a pattern that is correctly written to use it as a model. Maybe this way I will find what I am doing wrong. Link to comment Share on other sites More sharing options...
HartleySan Posted August 27, 2012 Share Posted August 27, 2012 The following should give you what you want: $pattern = '/^[A-Za-z1-3%]+$/'; This pattern matches both uppercase and lowercase letters, the numbers 1 to 3, and the percent sign (%). Note that I used +, so at least one character is required. An empty string will fail. You can change the + to * if you want to make an empty string pass. Link to comment Share on other sites More sharing options...
Recommended Posts