Jump to content
Larry Ullman's Book Forums

Regular Expressions Not Working


Recommended Posts

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

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

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>';
}

  • Upvote 1
Link to comment
Share on other sites

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

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

  • 5 weeks later...

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

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

  • 2 weeks later...

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...